javaScript window timing events

the javaScript code can be executed in time-intervals.

the window object two methods setinterval and settimeout methods are used to perform this task.

  • setinterval()  : execute a function, over and over again, at specified time interval
  • settimeout() : execute a function, once after waiting a specified number of milliseconds

JavaScript window setInterval() method

the setinterval() method calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.

the first parameter of setinterval() should be a function.

syntax:

          returnValue  =  window.setinterval('expression' , milliseconds)
  • expression   : a function which you want to be called repeatedly
  • milliseconds : integer that specifies the number of milliseconds
  • returnvalue : integer. returns an identifier that cancels the timer with the clearinterval                       method

to display current time we use the setinterval() method to execute the function once every 1 second look below example

function     time(){
   
                           var   time      =  new date();
                        
                           var   time_str =  time.toLocaleString();
                        
document.getElementById('top').innerhtml="the  current time = "+ time_str ;
                     
                     }
                                   
  // to pass a function as a string, be sure to append the function name with parentheses.
                                               
   var        timer_id  =  window.setInterval("time()", 1000 ) ;

                      // when passing a function pointer, do not include the parentheses.
                   
  var         timer_id  = window.setInterval(time , 1000) ;   

 


how stop the time execution ?

JavaScript Window clearInterval method

the window object clearinterval() method is used to stop the time execution which started using the setinterval method.

the clearinterval() cancels repeated action which was set up using setinterval.

syntax:

         window.clearinterval(timerid)
  • timerid : integer, that specifies the interval to cancel.this value must have been previously                 returned by the setinterval() method.
function     time(){
   
                           var   time      =  new date();
                        
                           var   time_str =  time.toLocaleString();
                        
document.getElementById('top').innerhtml="the  current time = "+ time_str ;
                     
                     }
                                   
  // to pass a function as a string, be sure to append the function name with parentheses.
                                               
   var        timer_id  =  window.setInterval("time()", 1000 ) ;

function   stop_time(){

                                 clearinterval(timer_id) ;    }


window  setTimeout()  method

the window.settimeout() method calls a function or executes a code snippet after a specified time delay.

syntax:

         var   timerid  =  window.settimeout(fun,  millis) 
  • fun   : is the function you want to execute after specified milliseconds.
  • millis : integer that specifies the number of milliseconds.
function      alerts(){ window.alert("hello i am alert control by timer event") ;}
                        
  // to pass a function as a string, be sure to append the function name with parentheses.
                                               
   var       timer_id  =  window.setTimeout("alerts()", 4000 ) ;

window  clearTimeout()  method

the window.cleartimeout() method is used to stop the execution of the function specified in the settimeout() method.

        window.cleartimeout(timerid)
  • timerid : integer that specifies the time-out returned by a previous call to the settimeout method.
function     alerts(){   window.alert("hello i am alert control by timer event") ;}

function     stop_alert(){ window.clearTimeout(timer_id);}
                        
  // to pass a function as a string, be sure to append the function name with parentheses.
                                               
   var       timer_id  =  window.setTimeout("alerts()", 5000 ) ;