Issues with setTimeout() inside jQuery .each

后端 未结 5 766
猫巷女王i
猫巷女王i 2021-01-21 06:33

The following code will not work properly. I\'ve tried different variations & searching everywhere but no luck.

i = 1;
var timer = new Array();
jQuery(\'a\')         


        
相关标签:
5条回答
  • 2021-01-21 07:00

    try:

    <html>
    <body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <a href="#">a</a>
    <a href="#">a</a>
    <a href="#">a</a>
    <a href="#">a</a>
    <a href="#">a</a>
    <a href="#">a</a>
    <script>
    i = 1;
    var timer = new Array();
        jQuery('a').each(function($) {
        i++;
        timer[i] = setTimeout(jQuery.proxy(function(){jQuery(this).remove();},this), i * 500);
    })
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-21 07:01

    Wrap remove element with a function

    i = 1;
    var timer = new Array();
    jQuery('a').each(function($) {
        i++;
        var thiz = jQuery(this);
        timer[i] = setTimeout(function() { thiz.remove(); }, i * 5000);
    })
    
    0 讨论(0)
  • 2021-01-21 07:01

    setTimeout accepts javascript statements not the return value of jQuery(this).remove() :P See this link

    You can just function(){stuff} but not sure if jQuery(this) will be processed when you want it to.

    0 讨论(0)
  • 2021-01-21 07:11

    Felix has already hinted at the issue in the comments, but I will expand.

    timer[i] = setTimeout(jQuery(this).remove(), i * 5000)
    

    Your issue lies in the fact that you are invoking jQuery(this).remove() and passing the return value of this to your setTimeout. The assumption is that you are intending for this to run when the timeout expires. If that is the case, you need to wrap this in a function, so that function will be passed to setTimeout and executed when the timer expires.

    var $el = jQuery(this);
    
    timer[i] = setTimeout(function(){
        $el.remove()
    }, i * 5000)
    
    0 讨论(0)
  • 2021-01-21 07:13

    The first parameter to setTimeout (or setInterval) needs to be a reference to a function (or a string, but you don't want to use the string syntax).

    Instead of passing a function as a parameter you are calling a function and passing its result. If you remove the parentheses you'll pass a reference to the function:

    timer[i] = setTimeout(jQuery(this).remove, i * 5000) 
    

    But then you'll start having trouble with this being the wrong thing at the time the function actually runs. Try something like this:

    var i = 1,
        timer = [];
    jQuery('a').each(function($) {
        i++;
        var $this = jQuery(this);
        timer[i] = setTimeout(function() {$this.remove();}, i * 5000)
    })
    

    This takes advantage of the way closures work in that the anonymous function passed to setTimeout will have access to the $this variable at the time it is run even though the function in which $this is declared will have finished executing by then.

    Note that it is better to declare arrays with [] than new Array().

    Note also that you initialise i to 1, then increment it before using it such that the first element that you add to your array will be timer[2]. You probably should initialise it to 0 and then increment it after setting each timer.

    0 讨论(0)
提交回复
热议问题