「setimeout」を使用してオブジェクト自体を呼び出す方法
-
12-09-2019 - |
質問
なぜ私は使えないのですか setTimeout
JavaScriptオブジェクトで?
Message = function () {
...
...
this.messageFactory = ...
this.feedbackTag = document.getElementById('feedbackMessages');
this.addInfo = function (message) {
var info = this.messageFactory.createInfo(message); // create a div
this.feedbackTag.appendChild(info);
setTimeout('this.feedbackTag.removeChild(info)', 5000);
// why in here, it complain this.feedbacktag is undefined ??????
};
}
Steveの解決策をありがとう、今ではコードが以下にある場合に機能します...以前の「この」が実際にSettimeout内の関数を指していたため、メッセージを再検索することはできません。
Message = function () {
...
...
this.messageFactory = ...
this.feedbackTag = document.getElementById('feedbackMessages');
this.addInfo = function (message) {
var info = this.messageFactory.createInfo(message); // create a div
this.feedbackTag.appendChild(info);
var _this = this;
setTimeout(function() { _this.feedbackTag.removeChild(info); }, 5000);
};
}
しかし、これを行う場合、なぜそれが機能しないのですか:
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);
}
}
解決
この行を交換してみてください:
setTimeout('this.feedbackTag.removeChild(info)', 5000);
これらの2つの行で:
var _this = this;
setTimeout(function() { _this.feedbackTag.removeChild(info); }, 5000);
ノート:
決して合格しないでください setTimeout
これが呼び出すように、文字列 eval
(必要に応じて使用する必要があります)。代わりに、パス setTimeout
関数参照(これは匿名関数になる可能性があります)。
最後に、常に確認してください this
キーワードは、あなたがそれが指していると思うものを指しています(参照してください http://www.alistapart.com/articles/getoutbindingsituations).
質問2への対処:
私は通常の機能のために、 this
に設定されています window
オブジェクト - それらが宣言されている場所に関係なく。したがって、コードを別の関数に移動しても問題は解決しません。
他のヒント
より厄介な方法は、ただ通過することです これ タイムアウトで呼び出されている関数の引数として:
function delayRemove(obj) {
setTimeout(function(_this) {
_this.feedbackTag.removeChild(obj);
}, 5000, this);
}
本当に合格する必要があります OBJ 議論としても、それが範囲内であることを確認するためだけに(パラメーターの数は無制限です):
function delayRemove(obj) {
setTimeout(function(_this, removeObj) {
_this.feedbackTag.removeChild(removeObj);
}, 5000, this, obj);
}
html5およびnode.jsは拡張されました setTimeout
コールバック関数に渡されるパラメーターを受け入れる機能。次のメソッド署名があります。
setTimeout(callback, delay, [param1, param2, ...])
として setTimeout
実際にはJavaScript機能ではありません 結果はブラウザによって異なる場合があります。サポートの具体的な詳細は見つかりませんでしたが、これはHTML5仕様にあると言ったように。
あなたの最後の質問に答えるために:「なぜ私たちがこれをするならば、なぜそれが機能しないのか」:
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);
}}
未定義の変数を渡しているため、機能していません(info
)定義された変数の代わりに(obj
)。これが修正された関数です:
function delayRemove(obj) {
var _this = this;
setTimeout(function() { _this.feedbackTag.removeChild(obj); }, 5000);}