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>
Powered by Linode.