I am trying to wrap up a plugin that I am writing but am stumped on a very trivial problem that I cant seem to find a solution for. I have tracked down almost every forum and related problem discussion. Any ways here is what I want to happen, my plugin has a timer that I would like the user to be able to handle its termination.
(function ($) {
...
var defaults = {
callback : function(){},
crossfadeTime : 1000,
easing : 'linear',
running : 'true', // Here is where I would like the user to send a parameter
// to invoke a different result. i.e. 'false' => clearing
// the timer. Such as...
var timer = setInterval(function(){
// Do Something;
},3000);
if(defaults.running == 'false'){
clearInterval(timer);
}
Do you really need them to be able to stop the timer by setting a variable? Could you do it by letting them call a function like this?
Otherwise, somethign like this would work:
It uses setTimeout instead of interval, and doesn’t reset the timeout if running is false.
If you’re dead set on using setInterval, this will do the trick: