SetTimeout and setinterval have the same syntax. They all have two parameters, one is the code string to execute, and one is the time interval in milliseconds, and the code executes after that time period.
However, there are differences between the two functions:
SetInterval after executing the code once, after that fixed time interval, it also automatically repeats the code, and settimeout only executes the code once.
Difference:
Window.settimeout ("function", time);//Set a timeout object, execute once, no cycle
Window.setinterval ("function", time);//Set a timeout object, period = ' interaction times '
Stop timing:
Window.cleartimeout (object) clears the SetTimeout object that has been set
Window.clearinterval (object) clears the SetInterval object that has been set
Perrefresh (); function Perrefresh () { varnew Date (); Alert ("The time is:" + today.tostring ()); SetTimeout ("ShowTime ()", "the");}
Once the function Perreflesh is called, the time is displayed every 5 seconds
SetInterval ("Perrefresh ()", "the"); function Perrefresh () { varnew Date (); Alert ("The time is:" + today.tostring ());}
But setinterval is not bound by the function that he calls, it simply repeats the function once every time.
Whenever SetInterval ("Perrefresh ()", 5000) of this function is called, the function Perrefresh is executed every 5 seconds.
If it is required to perform an action precisely after every fixed interval, it is best to use setinterval, and if you do not want to cause mutual interference with successive calls, especially if the call of a function requires heavy computation and long processing time, then it is best to use settimeout.
SetInterval continuously executes the specified code until it calls Clearinterval to clear the timer object
SetTimeout executes the specified code once and clears the timer object with Cleartimeout
Both SetInterval and settimeout return timer object identifiers for Clearinterval and Cleartimeout calls
SetTimeout and SetInterval detailed