开发者社区> 问答> 正文

JavaScript中,如何在setTimeout函数中调用一个class中的函数

function create_plant(name,position_first,position_second)
{
this.name=name;
this.position_first=position_first;
this.position_second=position_second;
this.plant=function(a){
if((test==0)&&(this.name))
{
choose="";
test+=1;
var b=document.createElement("img");
var plant_name="img/plant_gif/"+this.name+".gif";
b.setAttribute("src", plant_name);
a.appendChild(b);
this.wait_for_time();
test=test-1;

}
}
this.wait_for_time=function(){
can_move=0; 
var picture=this.name+"_gif";
document.getElementById(picture).style.display="none";
setTimeout("this.time_out()", 3*1000);
}
this.time_out=function(){
var old_picture=document.getElementById(this.name);
old_picture.src="img/plant/"+this.name+".png";

}
}

如上,我想在这个class中的wait_for_time函数中调用setTimeout,希望能够在3秒钟后调用这个class中的time_out方法,可是用this.time_out没法令time_out执行,求能够调用time_out的解决方法

展开
收起
小旋风柴进 2016-03-17 10:14:11 2143 0
1 条回答
写回答
取消 提交回答
  • 把那个wait_for_time改成这样就行了

      this.wait_for_time = ()=> {
        can_move = 0;
        var picture = this.name + "_gif";
        document.getElementById(picture).style.display = "none";
        setTimeout(()=>{
            this.time_out()
        }, 3 * 1000);
      }
    2019-07-17 19:04:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载