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
Post a Comment