Reputation: 401
I'd like to use a variable for the millisecond parameter in setTimeout.
HTML
<ul>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
</ul>
jQuery
var wait = $("li:last").index()*4000;
document.write(wait+'<br>');
setTimeout (argument.callee, wait);
The code should calculate how long to wait depending on how many list items there are and then repeat the code. This should repeat infinitely. I've tried many variants, they all create errors. I'm sure it's something obvious. Any help?
Here's the fiddle http://jsfiddle.net/sZ3KY/
Upvotes: 1
Views: 218
Reputation: 507
I think your problems lies in the fact that setTimeout requires a function. See:
var wait = $("li:last").index()*4000;
document.write(wait+'<br>');
setTimeout (function(){alert("I've waited "+wait/1000+"seconds before doing this")}, wait);
Upvotes: 0
Reputation: 227220
First off, don't use document.write
. It will clear the entire page, and replace it. Second, what are you trying to do here? What do you expect argument.callee
to be here?
Your code works fine, if you give it an actual function:
var wait = $("li:last").index()*4000;
setTimeout (function(){
alert('test');
}, wait);
Upvotes: 0
Reputation: 250882
The error isn't with the wait. The wait works fine...
var wait = $("li:last").index()*1000;
document.write(wait+'<br>');
setTimeout (function () { alert('Works') }, wait);
The problem is that argument.callee
isn't defined in your JS Fiddle.
Upvotes: 0
Reputation: 66663
It is arguments.callee, not argument.callee
Check this working demo: http://jsfiddle.net/sZ3KY/3/
Code:
var wait = $("li:last").index()*500;
function doIt() {
document.write(wait+'<br>');
setTimeout (arguments.callee, wait);
}
doIt();
Upvotes: 3
Reputation: 598
are you wanting to delay the printing of wait? if so use a function inside you time out like so
var wait = $("li:last").index()*4000;
setTimeout (function() {
document.write(wait+'<br>');
}, wait);
Upvotes: 0