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, the two functions are still different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only one time to execute the code.
Difference:
var T1 = window.settimeout ("function", time);//Set a timeout object, execute once, no cycle
var t2 = window.setinterval ("function", time);//Set a timeout object, period = ' interaction times '
Stop timing:
Window.cleartimeout (t1) clears the SetTimeout object that has been set
Window.clearinterval (T2) Clears the SetInterval object that has been set
SetTimeout ("ShowTime ()", 5000);
function ShowTime () {var today = new Date (); $ ("#txtDate"). Text (today.tostring ());}
After 5 seconds, the call is not repeated.
SetInterval ("ShowTime ()"), function ShowTime () {var today = new Date (), $ ("#txtDate"). Text (today.tostring ());}
Called every 5 seconds after the call is repeated.
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
JS SetTimeout and SetInterval differences