0

How to make continue loop after certain condition meet?I mean need to restart loop again after certain condition value or range meet .

Jquery setinterval loop condition restart after certain condition meet but below my code not working even not restart.

//Global  interval:any; 
    <i>public autoCheckPagination(): void {
    clearInterval(this.interval);
    this.interval=setInterval(function(){
    if(pageCount<3){         //loop range till 3 
    if(count<1){
    count=5;
    pageNextClick.click();
    }
    document.getElementById("displayAutoCount").innerHTML = count+"";
   count--;
   pageCount++;
   }else if(pageCount==3){  //loop meet condition again but not restart  
   restart loop again 
  pageCount=1;
   clearInterval(this.interval);
   this.running=true;
   }

     },1000);

    }</i>

Setinterval loop not restart again if condition meet .

2 Answers 2

1

I would recommend using setTimeout if you want to loop through a function repeatedly. The demonstration below gives you all the tools you need to code the functionality you would like.

The loop is started on the click of a button, only continues if the checkbox is checked, and can be stopped on a click of a button.

The function to stop the loop is independent, so you can call it in multiple ways or at various points in your code.

// Create array for setTimeouts
var timeouts = [];


// Start loop on click of button
$("#startLoop").click(function() {

  // Add class that enables scroll
  $(this).addClass("show-scroll");

  // Stop all other timeouts
  var cleared = stopLoop();

  // Run your function
  yourFunction();

});


// Stop all timeouts when clicking stop loop button
$("#stopLoop").click(function() {

  // Stop all other timeouts
  var cleared = stopLoop();  
  console.log("Loop stopped by clicking button");

});


// You master function, that will repeatedly fire
function yourFunction() {

  // Demonstrate to user loop is continuing
  $("#dots").append(".");

  // Check if the checkbox is still checked
  if ($('#continueCheck').prop('checked')) {

    // Start a new setTimeout to continue loop
    timeouts.push(setTimeout(yourFunction, 1000));

  } else {

    // Stop the loop
    stopLoop();
    console.log("Loop stopped as checkbox is not checked.");

  }

}


// Indepenent function to end all timeouts
function stopLoop() {

  // Clear all setTimeouts
  for (var i = 0; i < timeouts.length; i++) {
    clearTimeout(timeouts[i]);
  }

  return true;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="startLoop">Start Loop</button>
<button id="stopLoop">Stop Loop</button>

<input type="checkbox" id="continueCheck" checked/> Continue loop

<hr>

<div id="dots"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

not restarted because you need to call autoCheckPagination() whenpageCount == 3

else if (pageCount == 3) {
  pageCount = 1;
  clearInterval(this.interval);
  this.running = true;
  autoCheckPagination(); // call it again here
}

3 Comments

When calling above method I'm getting error like:core.js:1673 ERROR TypeError: this.autoCheckPagination is not a function
how you call autoCheckPagination() outside the function? call it like that.
I'm calling function inside function (like recursion)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.