For reasons beyond me, I'm looking for a way to create a setInterval with a specific ID.
For example:
when you create a setInterval and assign it to a variable like so:
var myInterval = setInterval(function(){..},100);
myInterval is actually equal to a number.
so when you clear the interval you could either go clearInterval(myInterval), or use the number it is equal to.
myIntertval === 9;
clearInterval(9);
How can i make the myInterval === 20. and thus give my setInterval an ID of 20.
Is it even possible? Or am I simply being silly and should do everything the right way.
Is it even possible? Or am I simply being silly and should do everything the right way.
You should do it in the proper way (embrace refactoring!). Said this, you can fix it by "mapping" any ID to another value, just storing it in an object:
var myIntervals={};
myIntervals[0]=setInterval(function(){..},100);
myIntervals[20]=setInterval(function(){..},100);
myIntervals["hello"]=setInterval(function(){..},100);
Then you will have stored something like:
{ 0: <ID_first interval>,
20: <ID_another_interval>,
hello: <<ID_another_interval_more>
}
and then you can do:
clearInterval(myIntervals[requiredId]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With