Thursday, November 1, 2018

javascript - How to use setTimeout in constructor

Firstly, self is not something which exists anywhere (within that scope). Secondly, this within the setTimeout function does not refer to the current object. You also need to call rotateFor within the correct context.


Use something like:


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);
}

If you want to start the timeout within the constructor you can do the following:


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(,); //Parameters needed
}, 5000);

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...