简体   繁体   English

如何停止正在运行的自定义 jQuery 函数?

[英]How can I stop custom jQuery function which is running?

I have a custom jQuery function.我有一个自定义的 jQuery 函数。 When it runs every 5 seconds.当它每 5 秒运行一次时。

(function($) {
        $.fn.mycustomfunction = function() {
            interval = setInterval(function() {
                console.log("I am running every 5 seconds");
            }, 5000);
        }
        return this;
    };
})(jQuery);

$("#container").mycustomfunction();

I have a我有一个

clearInterval(interval);

to stop, but I also want to stop the function completely.停止,但我也想完全停止该功能。 How can I do that ?我怎样才能做到这一点 ?

Functions you add to this object will be attached to your object and Simple and naive solution will follow:您添加this对象的功能将附加到您的对象,简单而天真的解决方案将遵循:

(function($) {
    $.fn.mycustomfunction = function() {
        interval = setInterval(function() {
            console.log("I am running every 5 seconds");
        }, 1000);

      this.stop= function(){
        clearInterval(interval);
      }
      // another function 
      this.alert = function(msg){
           alert(msg)
      }
    return this;
};
})(jQuery);

to stop use停止使用

var feature = $("#container").mycustomfunction();
feature.stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM