Question

I'm using this 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!

Was it helpful?

Solution

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]);
    } 
});

OTHER TIPS

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top