jquery - Clear all instances of an interval -


is possible remove instances of interval?

code snippet:

function callinterval() {     theinterval = setinterval(function() {         console.log("interval called");     }, 1000); }  callinterval(); settimeout(callinterval, 4000);  settimeout(clearthem, 8000);  function clearthem() {     clearinterval(theinterval); } 

the code snippet above removes 1 interval, other continues.

the problem here overriding value of variable theinterval every time call callinterval, hold last reference.

one easy solution here use array hold references , remove them when calling clear

var theinterval = [];    function callinterval(test) {    theinterval.push(setinterval(function() {      snippet.log("interval called: " + test);    }, 1000));  }    callinterval(1);  settimeout(callinterval.bind(undefined, 2), 4000);    settimeout(clearthem, 8000);    function clearthem() {    theinterval.foreach(clearinterval);    clearinterval.length = 0; //hack clear array  }
<!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->  <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -