Why can\'t I use setTimeout in a javascript object?
Message = function () {
...
...
this.messageFactory = ...
this.fee
To answer your last question: "Why doesn`t it work if we do this":
Message = function () {
...
...
this.messageFactory = ...
this.feedbackTag = document.getElementById('feedbackMessages');
// public function
this.addInfo = function (message) {
var info = this.messageFactory.createInfo(message); // create a div
this.feedbackTag.appendChild(info);
delayRemove(info);
};
// private function
function delayRemove(obj) {
var _this = this;
setTimeout(function() { _this.feedbackTag.removeChild(info); }, 5000);
}}
It's not working because you are passing an undefined variable (info) instead of a defined variable (obj). Here is the corrected function:
function delayRemove(obj) {
var _this = this;
setTimeout(function() { _this.feedbackTag.removeChild(obj); }, 5000);}