Reputation: 255
when i change variable after load image, so in gameloop i have old value..
what i am doing wrong?
var symb ;
var Symbol=function(imgSrc) {
this.loaded=false;
this.img=new Image();
this.img.onload = function () {
this.loaded=true;
console.log("in loaded - " + this.loaded);
}
this.img.src=imgSrc;
}
Symbol.prototype = {
getLoaded: function()
{
return this.loaded;
}
}
symb=new Symbol("images/worm.jpg");
console.log("after loaded - " + symb.getLoaded());
setInterval(testSymbolsLoaded, 3000);
function testSymbolsLoaded() {
console.log("after 3 second loaded - " + symb.getLoaded());
}
this code return
after loaded - false
in loaded - true
after 3 second loaded - false
why the last value is false, when earlier was change to true??
thanks for help
Upvotes: 0
Views: 44
Reputation: 864
this.img.onload = function () {
this.loaded=true;
console.log("in loaded - " + this.loaded);
}
Within the function, this
is referring to the context of the image. You'll need to bind the function to the existing this
:
Fix:
this.img.onload = function () {
this.loaded=true;
console.log("in loaded - " + this.loaded);
}.bind(this);
Upvotes: 1