延迟后更改JavaScript对象变量

gee*_*h01 1 javascript

this.anim= true;
Run Code Online (Sandbox Code Playgroud)

如何将此对象的变量更改为秒falsex

zer*_*kms 5

this.anim = true;
var that = this; // you store the reference to a `this` in `that` variable,
                 // so you could use it in a callback function. You have
                 // to do that because it has its own `this` defined
setTimeout(function() {
    that.anim = false;
}, x * 1000);
Run Code Online (Sandbox Code Playgroud)

要么

this.anim = true;

(function(that){ // you create an IIFE (see http://tinyurl.com/js-iife)
    setTimeout(function() {
        that.anim = false;
    }, x * 1000);
}(this)); // and invoke it with `this` as a parameter, which will be available
          // as `that` in the function body
Run Code Online (Sandbox Code Playgroud)

  • 请添加评论你在代码中做了什么!所以,OP可以理解它!+1来自我的身边! (2认同)