I presume by "stop the function", you mean stop your interval timer.
To do that, you need to store the timer handle in the jQuery object (as a property using this
) and then add a new method to stop it.
You can do that my changing this line:
var timer = setInterval( plugin, params.param1);
to this:
this.pluginTimer = setInterval( plugin, params.param1);
and this line:
clearInterval(timer);
to this line:
clearInterval(this.pluginTimer);
And, then adding this method:
$.fn.stopPlugin = function() {
clearInterval(this.pluginTimer);
this.pluginTimer = null;
}
The whole block of code would be this after those changes:
(function( $ ) {
$.fn.plugin = function(params) {
params = $.extend( {param1: 10, param2: 5, param3:0, param4:100}, params);
this.stopPlugin();
this.pluginTimer = setInterval( plugin, params.param1);
var showTimes = params.param3;
var counter = 0;
function plugin() {
for (var i = 0; i < params.param2; i++) {
// code
}
if (counter == 0) { counter++; return; }
$('#div')
.stop()
.hide()
.filter( function() { return this.id.match('frame' + counter); })
.show();
if(counter == params.param2) {
counter = 0;
showTimes --;
if(showTimes == 0) {
this.stopPlugin();
}
} else {
counter++;
}
}
return this;
};
$.fn.stopPlugin = function() {
if (this.pluginTimer) {
clearInterval(this.pluginTimer);
this.pluginTimer = null;
}
}
})( jQuery );
Style-wise, I would recommend that you use meaningful names for your various parameters options instead of param1
, param2
, param3
and param4
. Pick names like count
and duration
that say what they are.
If you want the first click to start the plugin and the second click to stop it, you could make the call to plugin() alternate between starting and stopping with this code:
(function( $ ) {
$.fn.plugin = function(params) {
params = $.extend( {param1: 10, param2: 5, param3:0, param4:100}, params);
// if the timer was already running, then stop it and return
if (this.pluginTimer) {
this.stopPlugin();
return this;
}
this.pluginTimer = setInterval( plugin, params.param1);
var showTimes = params.param3;
var counter = 0;
function plugin() {
for (var i = 0; i < params.param2; i++) {
// code
}
if (counter == 0) { counter++; return; }
$('#div')
.stop()
.hide()
.filter( function() { return this.id.match('frame' + counter); })
.show();
if(counter == params.param2) {
counter = 0;
showTimes --;
if(showTimes == 0) {
this.stopPlugin();
}
} else {
counter++;
}
}
return this;
};
$.fn.stopPlugin = function() {
if (this.pluginTimer) {
clearInterval(this.pluginTimer);
this.pluginTimer = null;
}
}
})( jQuery );