I was allways certain that all the additional parameters you pass to setTimeout
will be forwarded to the callback:
function say(what) {
alert(what);
}
setTimeout(say, 500, "Hello world.");
MDN confirms this belief of mine:
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]);
But not so much the actual code:
function reload(cb) {
var req = new XMLHttpRequest();
req.open("GET", location.href);
//Call the callback on request completion
req.onload = function() {
console.log("Reloaded, calling callback.");
if(cb!=null)
cb();
else
console.log(" Callback is null.");
}
//Debug info
console.log("Sending request. Callback: "+cb);
console.log(" Arguments: "+arguments.length);
req.send();
}
function waitReload() {
console.log("Reload in 20 ms.");
//Sending 3 arguments in 20ms
setTimeout(reload, 20, waitReload, "BLE", "Number 666");
}
waitReload();
This prints out:
Reload in 20 ms.
Sending request. Callback: undefined
Arguments: 0
Reloaded, calling callback.
Callback is null.
No arguments are sent to the callback function. Why? The first code doesn't work either, it alerts undefined
.