How to pass a variable on jQuery timeout
I am seriously fighting today.
I need to pass a variable or even better an object in timeOut as such (example):
$('.x').each(function()
{
setTimeout(function()
{
alert ($(this).attr('id'))
},10000);
});
Obviously what is happening is that timeOut has no reference to the original $(this)
Reference?
+2
a source to share
1 answer
this
is context sensitive (and is different from a_jQuery_object.each
than in window.setTimeout
, but its reference can be copied to another variable that is not context sensitive. Traditionally used that
for this purpose.
$('.x').each(function() {
var that = this;
setTimeout(function() {
alert ($(that).attr('id'))
},10000);
});
+5
a source to share