开发者

How can I pass a variable into jQuery timeout

I'm seriously struggling today.

I need to pass a variable, or even better an object, into a timeOut as such 开发者_运维技巧(example) :

$('.x').each(function() 
{
  setTimeout(function()
  {
    alert ($(this).attr('id'))
  },10000);
});

Obviously what happens is that the timeOut doesn't have reference to the original $(this)

Help ?


this is context sensitive (and is different in a_jQuery_object.each than it is in window.setTimeout, but its reference can be copied to a different variable that is not context sensitive. It is conventional to use that for this purpose.

$('.x').each(function() {
    var that = this; 
    setTimeout(function() { 
        alert ($(that).attr('id'))
    },10000); 
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜