Why can\'t I use setTimeout
in a javascript object?
Message = function () {
...
...
this.messageFactory = ...
this.fee
A neater way is to just pass this as an argument to the function being called in the timeout:
function delayRemove(obj) {
setTimeout(function(_this) {
_this.feedbackTag.removeChild(obj);
}, 5000, this);
}
You should really pass obj as an argument as well, just to make sure it is in scope (the number of parameters is unlimited):
function delayRemove(obj) {
setTimeout(function(_this, removeObj) {
_this.feedbackTag.removeChild(removeObj);
}, 5000, this, obj);
}
HTML5 and Node.js extended the setTimeout
function to accept parameters which are passed to your callback function. It has the following method signature.
setTimeout(callback, delay, [param1, param2, ...])
As setTimeout
isn't actually a JavaScript feature your results may vary across browsers. I couldn't find any concrete details of support, however as I said this is in the HTML5 spec.