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: Tue Aug 07 2012 11:28:42 GMT+0600 (Central Asia Standard Time)

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: Tue Aug 07 2012 11:28:42 GMT+0600 (Central Asia Standard Time)

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>
Interval will run in an interval, meaning over and over again. setTimeout will run only once.

Advertisements
By Rz Rasel Posted in jQuery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s