为了账号安全,请及时绑定邮箱和手机立即绑定

JavaScript中的setTimeout和“this”

JavaScript中的setTimeout和“this”

万千封印 2019-07-25 10:00:03
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


查看完整回答
反对 回复 2019-07-25
  • 3 回答
  • 0 关注
  • 496 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号