Javascript how to clear interval after specific time

skos picture skos · Jul 6, 2012 · Viewed 17.5k times · Source
setInterval("FunctionA()", 1000);

Now how do I clear this interval after exactly 5 seconds so that I can achieve -

var i = setInterval("FunctionA()", 1000);
(After 5 seconds)
clearInterval(i);

Answer

antyrat picture antyrat · Jul 6, 2012

You can do this using setTimeout function:

var i = setInterval(FunctionA ,1000);
setTimeout(function( ) { clearInterval( i ); }, 5000);