开发者

Stop a Function, jQuery

开发者 https://www.devze.com 2023-02-28 23:01 出处:网络
I\'m using th开发者_如何学Gois blink function (function($) { $.fn.blink = function(options) { var defaults = { delay:500 };

I'm using th开发者_如何学Gois blink function

(function($)
{
  $.fn.blink = function(options)
   {
       var defaults = { delay:500 };
       var options = $.extend(defaults, options);

       return this.each(function()
       {
           var obj = $(this);
           setInterval(
            function(){
               if($(obj).css("visibility") == "visible") {
                  $(obj).css('visibility','hidden');
               }
               else{
                  $(obj).css('visibility','visible');
               }
             },
            options.delay);
        });
    }
}(jQuery))

$("#bootUp p").blink({delay:300});  

I'd like for it to stop on window.load

I'm not sure how to do it, exactly? Any help would be much appreciated. Thanks!


Live Demo

You need to track the ints returned from setInterval and then clearInterval for each of them.

var intervals = new Array();

(function($)
{
  $.fn.blink = function(options)
   {
       var defaults = { delay:500 };
       var options = $.extend(defaults, options);

       return this.each(function()
       {
           var obj = $(this);
           intervals[intervals.length] = setInterval(
            function(){
               if($(obj).css("visibility") == "visible") {
                  $(obj).css('visibility','hidden');
               }
               else{
                  $(obj).css('visibility','visible');
               }
             },
            options.delay);
        });
    }
}(jQuery))

$("#bootUp p").blink({delay:300});
$("#bootUp2 p").blink({delay:300});

$(document).ready(function(){
    for(var i=0;i<intervals.length;i++){
        clearInterval(intervals[i]);
    } 
});


The most correct solution would be store the ids returned by setInterval, and then clear them all with clearInterval. However, a more simple/braindead approach is just:

$("#bootUp p").replaceWith($("#bootUp p").clone());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号