Discussion on setInterval of method in jQuery

  • 2020-07-21 06:48:07
  • OfStack

Definition and usage:

The setInterval() method can call a function or evaluate an expression for a specified period in milliseconds.

The setInterval() method calls the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as a parameter to the clearInterval() method.

var time = 0;

Use 1:


function jump(){
 ..................... // The function content 
}
time = setInterval("jump",5000); // each 5 A second call 1 Time function 

When you need to pause


  $("").hover(function(){
    clearInterval(time),function(){
    time = setInterval("jump",5000); 
    }  
  })

Use 2:


function autoPlay(){
  time = setInterval(function(){
 .....................    // The function content 
  },5000);
}
autoPlay();  // Call a function 

When you need to pause


   $("").hover(function(){
    clearInterval(time),function(){
    autoPlay();
    }  
  })

Conclusion:

The first way of usage is relatively clear. First, a function is set to call itself via setInterval, but it is difficult to call it elsewhere.

The second method looks a bit messy. Write a self-calling function inside setInterval, then attach a named function to it, and then automate it by calling a named function, which is more convenient elsewhere.

Above is purely personal view, hope great god people give directions more.


Related articles: