Thursday 1 November 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...