The difference between setTimeout and setInterval

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>

 


  • Nomi
    #1
    2011-11-23
    [Quote]
    Nice Explanation with Examples
2182,Nickname,Homepage or email,Comments here,Add comment