我有一个构造函数:
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 + ")"
});
};
我想将超时设置为rotateFor
。我尝试了这个:
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
不是存在于任何地方(在该范围内)的东西。其次,this
在setTimeout函数内不引用当前对象。您还需要rotateFor
在正确的上下文中调用。
使用类似:
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句