setInterval function execute some code at specified time-intervals.

In this source code:
– Basic syntax
– Alerts and notifications

The Basic Syntax:

setInterval(function(){                       
      ... you want to do ...
      },3000); // setInterval repeat the execution of the function each xxx milliseconds 

With an alert window every 3 seconds:

<script>
setInterval(function(){ // the function display every 3 seconds an alert window                         
      alert("Hello") // display the alert window
      },3000); // setInterval repeat the execution of the function each xxx milliseconds 
</script> 

Stop calling ‘setInterval’ on some event:

<script> 
$(document).ready(function(){
   
  var moveLeftId = null; // You have to store the return of 'setInterval' in a variable

  // START setInterval
  $("#element").mouseenter(function(){

    if (moveLeftId !== null) return; // Use to stop setInterval
		
	moveLeftId = setInterval(function(){ // You have to store the return of 'setInterval' in a variable
	... you want to do ...
	},20); 	
  });
  
  // STOP setInterval
  $("#element").mouseleave(function(){
    clearInterval(moveLeftId); // The return of 'setInterval' 
	moveLeftId = null;     // The return of 'setInterval' 
  });
  
});
</script> 

NOTICE: You have to store the return of ‘setInterval’ in a variable to stop it.

My official WebSite >