JavaScript中的setTimeout和“this”我有一个使用该setTimeout函数的方法,并调用另一个方法。在初始加载方法2工作正常。但是,在超时后,我收到一个错误,表示method2未定义。我在这做错了什么?例如:test.prototype.method = function(){
//method2 returns image based on the id passed
this.method2('useSomeElement').src = "http://www.some.url";
timeDelay = window.setTimeout(this.method, 5000);};test.prototype.method2 = function(name) {
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].id.indexOf(name) > 1) {
return document.images[i];
}
}};
3 回答
智慧大石
TA贡献1946条经验 获得超3个赞
在this你使用中setTimeOut通过自身的范围界定。var "foo = this;"在t est.prototype.method函数内部创建并使用foo。
添加回答
举报
0/150
提交
取消
