Javascript 如何在构造函数中使用setTimeout

Javascript 如何在构造函数中使用setTimeout,javascript,Javascript,我有一个构造器: function Domino() { var self = this; this.myElement = $("#smth"); this.rotation = 0; this.rotateFor = function (deg, scl) { this.rotation += deg; this.scale = scl; this.myElement.find(".domino-view").css({ transform: "r

我有一个构造器:

function Domino() {

var self = this;
this.myElement = $("#smth");

this.rotation = 0;
this.rotateFor = function (deg, scl) {
    this.rotation += deg;
    this.scale = scl;
    this.myElement.find(".domino-view").css({
        transform: "rotate(" + self.rotation + "deg)  scale(" + self.scale + ")"
    });
};
我想为设置旋转超时。我试过这个:

this.rotateFor = function (deg, scl) {
    this.rotation += deg;
    this.scale = scl;
    this.myElement.find(".domino-view").css({
        transform: "rotate(" + self.rotation + "deg)  scale(" + self.scale + ")"
    });
}
this.start = function(){
self.timeout = setTimeout(function(){this.rotateFor()}, 5000)
}


然后我这样称呼它:
something.start()
,但它仍然不起作用。如何在此构造函数中设置超时?

您正在使用
self
创建一个实例变量,并在
setTimeout
,它不会引用正确的对象实例

function Domino() {

   var self = this;
   this.myElement = $("#smth");

   this.rotation = 0;
   this.rotateFor = function (deg, scl) {
       this.rotation += deg;
       this.scale = scl;
       this.myElement.find(".domino-view").css({
           transform: "rotate(" + self.rotation + "deg)  scale(" + self.scale + ")";
       });
   };

   this.start = function(){
      this.timeout = setTimeout(function(){
          // Here is where the self variable benefits you:
          self.rotateFor();
      }, 5000);
   }
 }

首先,
self
不是任何地方(在该范围内)都存在的东西。其次,
setTimeout函数中的此
不引用当前对象。您还需要在正确的上下文中为调用旋转

使用类似于:

this.start = function(deg,scl){
    var self = this; //Must specify what self is.
    this.timeout = setTimeout(function(){ //This outside (self also works since we defined it)
          self.rotateFor(deg,scl); 
    }, 5000);
}
如果要在构造函数中启动超时,可以执行以下操作:

function Domino() {

var self = this;
this.myElement = $("#smth");

this.rotation = 0;
this.rotateFor = function (deg, scl) {
    this.rotation += deg;
    this.scale = scl;
    this.myElement.find(".domino-view").css({
        transform: "rotate(" + self.rotation + "deg)  scale(" + self.scale + ")"
    });
};
setTimeout(function(){ 
    self.rotateFor(<p1>,<p2>); //Parameters needed
}, 5000);
函数Domino(){
var self=这个;
this.myElement=$(“#smth”);
这是旋转=0;
this.rotateFor=功能(度,scl){
这是旋转+=度;
本量表=症状自评量表;
这个.myElement.find(“.domino视图”).css({
变换:“旋转(“+self.rotation+”度)比例(“+self.scale+”)
});
};
setTimeout(函数(){
self.rotateFor(,);//需要参数
}, 5000);
您需要绑定以下内容:

setTimeout(function(){this.rotateFor()}.bind(this), 5000);

@JamesThorpe这不是问题所在,因为OP在回调中正确地传递了
this
。但是他在调用
self.timeout
时可能没有传递正确的
this
。@Alnitak这就是这个dupe所涵盖的内容-回调中包含的
this
传递给
setTimeout
?OP已经有了
self
,只需要根据dupe使用它。您不需要将this.rotateFor调用包装到函数中。您只需编写self.timeout=setTimeout(this.rotateFor,5000);不过,请记住,您只是在这里设置调用timeout()时发生的情况。您实际上需要确保timeout()调用以使函数的rotateFor启动。@Alnitak:他没有在回调中正确传递
this
。它使用
this
而不是
self
,并且没有绑定。@ManoDestra我想OP希望能够控制
setTimeout()的时间
开始,因此它被包装在一个函数中。
self
来自
var self=this;
,这很好。
self
在第一个代码块中声明,而不是只包含rotate函数和调用它的尝试的较小副本。较小的更新。需要可以在this.start fu中传递rotateFor参数否则我不知道你从哪里得到它们。对于第二种情况,我不知道你从哪里得到它们。