3

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').each(function($) {
    i++;
    timer[i] = setTimeout(jQuery(this).remove(), i * 5000)
})
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Francis Kim
  • 4,235
  • 4
  • 36
  • 51
  • 6
    *The following code will not work properly.* What do you want it to do? I assume your problem is that you are calling `jQuery(this).remove()` immediately. – Felix Kling Jan 10 '12 at 01:15
  • 1
    Also note that the first argument passed to `each` is actually the index of the DOM element in the set of selected elements. That means you don't have to maintain a separate counter. [More information in the documentation](http://api.jquery.com/each/). – Felix Kling Jan 10 '12 at 01:43

5 Answers5

8

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);
})
Bon Espresso
  • 693
  • 3
  • 5
4

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.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
3

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)
James Montagne
  • 77,516
  • 14
  • 110
  • 130
0

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>
al01
  • 122
  • 1
  • 4
-1

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.

chopper
  • 6,649
  • 7
  • 36
  • 53
NoName13
  • 166
  • 9