I'm working on a jQuery plugin that utilises JavaScript intervals. However, when the plugin is called again with the argument running
as false
, then I want the previous interval called on that particular DOM element to be cleared.
Here is some shortened code:
(function ($) {
$.fn.examplePlugin = function (running) {
return this.each(function () {
if (running === false) {
// Clear the previous interval assigned to this DOM element
} else {
setInterval(function () {
// Interval code
}, 50);
}
return this;
});
};
})(jQuery);
How is this possible? I thought of using global variables to store the intervals but I don't really want to do that. I also don't have any clue how to assign an interval to a particular DOM element.