Могу ли я предложить это в качестве общего решения:
function sleep (ms, args, obj) {
/* Called at the top of a function to invoke a delay in processing that
function
Returns true if the function should be executed, and false if the sleep
timer is activated.
At the end of the sleep tiemout, the calling function is re-called by
calling it with "apply (obj, args || [])".
*/
var caller = sleep.caller;
if (caller.sleepTimer) {
/* if invoked from timeout function, delete timer and return false */
delete caller.sleepTimer;
return true;
}
/* Create timer and re-call caller at expiry */
caller.sleepTimer = window.setTimeout (function () {
caller.apply (obj || null, args || []);
},ms);
return false;
}
Примеры использования:
function delayed () {
if (!sleep (5000)) return;
document.body.innerHTML += "<p>delayed processing</p>";
}
function delayed_args (a, b) {
if (!sleep (10000, arguments)) return;
document.body.innerHTML += "<p>args : (" + a + ", " + b + ")</p>";
}
function delayed_method_args (a,b) {
if (!sleep (20000, arguments, this)) return;
document.body.innerHTML += "<p>method_args " + this + " (" + a + ", " + b + ")</p>";
}
Проверено на Opera и Firefox 3.0