Can often be used to refresh an expression.
Example 1
| The code is as follows |
Copy Code |
<div id= "a" ></div> <div id= "B" ></div> <script type= "Text/javascript" > SetTimeout ("document.getElementById (' a"). Innerhtml=new Date (). getseconds (); ", 1000); SetInterval ("document.getElementById (' B '). Innerhtml=new Date (). getseconds ();", 1000); </script> |
SetInterval () case
| The code is as follows |
Copy Code |
var leftseconds = 10; var intervalid; $ (function () { $ ("#btnReg"). attr ("Disabled", true); Intervalid = SetInterval ("Countdown ()", 1000); }); function Countdown () { if (Leftseconds <=0) { $ ("#btnReg"). Val ("submit"); $ ("#btnReg"). attr ("disabled", false); Clearinterval (Intervalid); Return }else{ leftseconds--; $ ("#btnReg"). Val ("Please read carefully" + Leftseconds + "seconds"); } } SetTimeout () settimeout (function () {Alert " Hello world");},1000)
|
A delay of 1 seconds after execution to this sentence pops up the alert window. So look at this paragraph again:
| The code is as follows |
Copy Code |
function A () { settimeout (function () {alert (1)},0); Alert (2); } A (); |
Difference Summary
Using setinterval and setting the delay function settimeout very similar.
SetTimeout is used to delay a period of time and then perform an operation.
SetTimeout ("function", time) sets a timeout object
SetInterval ("function", time)//Set a timeout object
SetInterval for automatic repetition, settimeout will not repeat.
Cleartimeout (object) clears the SetTimeout object that has been set
Clearinterval (object) clears the SetInterval object that has been set