Javascript Reference
Categories: window

The difference between setTimeout and setInterval

@Sept. 9, 2008, 2:40 a.m.

setTimeout: Run once and then stop.

Code sample: var timerA = setTimeout("AFunction()",1000);

How to loop: function AFunction(){/*..Some codes..*/ timerA = setTimeout("AFunction()",1000);}

How to stop: clearTimeout(timerA);

Example: timer A

Example Codes:
<i id="lblstdate">timer A</i>
<script type="text/javascript">
function AFunction(){
document.getElementById('lblstdate').innerHTML=(new Date()).toString();
setTimeout("AFunction()",1000)
}
AFunction();
</script>


setInterval: Run by loop.

Code sample: var timerB = setInterval("BFunction()",1000);

How to loop: It loops automatically.

How to stop: clearInterval(timerB);

Example: timer B

Example codes:
<i id="lblsidate">timer B</i>
<script type="text/javascript">
function BFunction(){
document.getElementById('lblsidate').innerHTML=(new Date()).toString();
}
var timerB = setInterval("BFunction()",1000);
//clearInterval(timerB)
</script>

 

Hello gakaa.com owner, Your posts are always a great source of knowledge.
Kenton Trenwith - 5 days
lipitor over the counter <a href="https://lipiws.top/">purchase atorvastatin generic</a> atorvastatin 80mg generic
Oogudm - 10 months
buy lipitor 20mg <a href="https://lipiws.top/">order generic lipitor 20mg</a> order atorvastatin 40mg sale
Bglfsq - 10 months

Powered by Linode.