setTimeout - Pass variable as time

孤街醉人 提交于 2019-12-11 11:24:12

问题


I have a setTimeout, and I want to be able to use a variable as the timer:

var that = this;
var time = this.spawnTime;

setTimeout( function(time){ 
    that.SpawnCounter();
}, time);

This doesn't seem to do the trick. Any ideas why?

Thanks


回答1:


var that = this;
var time = 1000;

setTimeout( function(time){ 
    that.SpawnCounter();
}, time);

The code above should work. Maybe the problem is that this.spawnTime is not numeric or invaild.



来源:https://stackoverflow.com/questions/15559739/settimeout-pass-variable-as-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!