javascript setInterval gives error when called on an array object [duplicate]












-3
















This question already has an answer here:




  • How to access the correct `this` inside a callback?

    10 answers




I am creating a game in javascript. When user clicks UP arrow it launches a missile. User should be able to click the UP arrow and each click should launch a new missile. I have a 'missile' object and an array of 'missile's. Missile object has a launch function.



The below line gives me an error when used inside setInterval. It works without issue when I comment out the setInterval.



Error message: game.js:21




Uncaught TypeError: Cannot read property 'style' of undefined at game.js:21




Line with error: this.image.style.left=this.leftPos+'px';



var missile = {
leftPos: 0,
bottomPos: 0,
image: null,
id: null,
launch: function () {
setInterval(function () {
this.image.style.left = this.leftPos + 'px';
this.image.style.bottom = this.bottomPos + 'px';
this.leftPos += 0.5;
this.bottomPos += 1;
}, 10);
}
};


Somewhere down I call this:
currMissile=missiles[missiles.length-1];
currMissile.launch();










share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 2:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • ‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

    – user2864740
    Nov 25 '18 at 2:12













  • See also: stackoverflow.com/questions/20279484/…

    – Mark Meyer
    Nov 25 '18 at 2:14
















-3
















This question already has an answer here:




  • How to access the correct `this` inside a callback?

    10 answers




I am creating a game in javascript. When user clicks UP arrow it launches a missile. User should be able to click the UP arrow and each click should launch a new missile. I have a 'missile' object and an array of 'missile's. Missile object has a launch function.



The below line gives me an error when used inside setInterval. It works without issue when I comment out the setInterval.



Error message: game.js:21




Uncaught TypeError: Cannot read property 'style' of undefined at game.js:21




Line with error: this.image.style.left=this.leftPos+'px';



var missile = {
leftPos: 0,
bottomPos: 0,
image: null,
id: null,
launch: function () {
setInterval(function () {
this.image.style.left = this.leftPos + 'px';
this.image.style.bottom = this.bottomPos + 'px';
this.leftPos += 0.5;
this.bottomPos += 1;
}, 10);
}
};


Somewhere down I call this:
currMissile=missiles[missiles.length-1];
currMissile.launch();










share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 2:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • ‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

    – user2864740
    Nov 25 '18 at 2:12













  • See also: stackoverflow.com/questions/20279484/…

    – Mark Meyer
    Nov 25 '18 at 2:14














-3












-3








-3









This question already has an answer here:




  • How to access the correct `this` inside a callback?

    10 answers




I am creating a game in javascript. When user clicks UP arrow it launches a missile. User should be able to click the UP arrow and each click should launch a new missile. I have a 'missile' object and an array of 'missile's. Missile object has a launch function.



The below line gives me an error when used inside setInterval. It works without issue when I comment out the setInterval.



Error message: game.js:21




Uncaught TypeError: Cannot read property 'style' of undefined at game.js:21




Line with error: this.image.style.left=this.leftPos+'px';



var missile = {
leftPos: 0,
bottomPos: 0,
image: null,
id: null,
launch: function () {
setInterval(function () {
this.image.style.left = this.leftPos + 'px';
this.image.style.bottom = this.bottomPos + 'px';
this.leftPos += 0.5;
this.bottomPos += 1;
}, 10);
}
};


Somewhere down I call this:
currMissile=missiles[missiles.length-1];
currMissile.launch();










share|improve this question

















This question already has an answer here:




  • How to access the correct `this` inside a callback?

    10 answers




I am creating a game in javascript. When user clicks UP arrow it launches a missile. User should be able to click the UP arrow and each click should launch a new missile. I have a 'missile' object and an array of 'missile's. Missile object has a launch function.



The below line gives me an error when used inside setInterval. It works without issue when I comment out the setInterval.



Error message: game.js:21




Uncaught TypeError: Cannot read property 'style' of undefined at game.js:21




Line with error: this.image.style.left=this.leftPos+'px';



var missile = {
leftPos: 0,
bottomPos: 0,
image: null,
id: null,
launch: function () {
setInterval(function () {
this.image.style.left = this.leftPos + 'px';
this.image.style.bottom = this.bottomPos + 'px';
this.leftPos += 0.5;
this.bottomPos += 1;
}, 10);
}
};


Somewhere down I call this:
currMissile=missiles[missiles.length-1];
currMissile.launch();





This question already has an answer here:




  • How to access the correct `this` inside a callback?

    10 answers








javascript setinterval






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 2:11







Golden State

















asked Nov 25 '18 at 2:07









Golden StateGolden State

11




11




marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 2:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 2:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • ‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

    – user2864740
    Nov 25 '18 at 2:12













  • See also: stackoverflow.com/questions/20279484/…

    – Mark Meyer
    Nov 25 '18 at 2:14



















  • ‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

    – user2864740
    Nov 25 '18 at 2:12













  • See also: stackoverflow.com/questions/20279484/…

    – Mark Meyer
    Nov 25 '18 at 2:14

















‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

– user2864740
Nov 25 '18 at 2:12







‘this’ is not what you think it is. The function supplied to setInterval is not executed on the same object context. Search for “javascript self this” or “javascript bind”.

– user2864740
Nov 25 '18 at 2:12















See also: stackoverflow.com/questions/20279484/…

– Mark Meyer
Nov 25 '18 at 2:14





See also: stackoverflow.com/questions/20279484/…

– Mark Meyer
Nov 25 '18 at 2:14












1 Answer
1






active

oldest

votes


















0














The context this is not referencing to the object missile, further, I can see you're filling an array with the same object multiple times, this means all indexes in the array are pointing to the same object which relies on errors.



You should instantiate a new object every time the user presses UP.



For example






var Missile = function() {
this.leftPos = 0;
this.bottomPos = 0;
this.image = { // Example
style: {
left: 4,
bottom: 34
}
};
this.id = null;
this.launch = function() {
setInterval(() => { // Use arrow function to get access to Missile lexical context.
this.image.style.left = this.leftPos + 'px';
this.image.style.bottom = this.bottomPos + 'px';
this.leftPos += 0.5;
this.bottomPos += 1;
console.log(this.image.style.left);
}, 500);
};
};

new Missile().launch();

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The context this is not referencing to the object missile, further, I can see you're filling an array with the same object multiple times, this means all indexes in the array are pointing to the same object which relies on errors.



    You should instantiate a new object every time the user presses UP.



    For example






    var Missile = function() {
    this.leftPos = 0;
    this.bottomPos = 0;
    this.image = { // Example
    style: {
    left: 4,
    bottom: 34
    }
    };
    this.id = null;
    this.launch = function() {
    setInterval(() => { // Use arrow function to get access to Missile lexical context.
    this.image.style.left = this.leftPos + 'px';
    this.image.style.bottom = this.bottomPos + 'px';
    this.leftPos += 0.5;
    this.bottomPos += 1;
    console.log(this.image.style.left);
    }, 500);
    };
    };

    new Missile().launch();

    .as-console-wrapper { max-height: 100% !important; top: 0; }








    share|improve this answer




























      0














      The context this is not referencing to the object missile, further, I can see you're filling an array with the same object multiple times, this means all indexes in the array are pointing to the same object which relies on errors.



      You should instantiate a new object every time the user presses UP.



      For example






      var Missile = function() {
      this.leftPos = 0;
      this.bottomPos = 0;
      this.image = { // Example
      style: {
      left: 4,
      bottom: 34
      }
      };
      this.id = null;
      this.launch = function() {
      setInterval(() => { // Use arrow function to get access to Missile lexical context.
      this.image.style.left = this.leftPos + 'px';
      this.image.style.bottom = this.bottomPos + 'px';
      this.leftPos += 0.5;
      this.bottomPos += 1;
      console.log(this.image.style.left);
      }, 500);
      };
      };

      new Missile().launch();

      .as-console-wrapper { max-height: 100% !important; top: 0; }








      share|improve this answer


























        0












        0








        0







        The context this is not referencing to the object missile, further, I can see you're filling an array with the same object multiple times, this means all indexes in the array are pointing to the same object which relies on errors.



        You should instantiate a new object every time the user presses UP.



        For example






        var Missile = function() {
        this.leftPos = 0;
        this.bottomPos = 0;
        this.image = { // Example
        style: {
        left: 4,
        bottom: 34
        }
        };
        this.id = null;
        this.launch = function() {
        setInterval(() => { // Use arrow function to get access to Missile lexical context.
        this.image.style.left = this.leftPos + 'px';
        this.image.style.bottom = this.bottomPos + 'px';
        this.leftPos += 0.5;
        this.bottomPos += 1;
        console.log(this.image.style.left);
        }, 500);
        };
        };

        new Missile().launch();

        .as-console-wrapper { max-height: 100% !important; top: 0; }








        share|improve this answer













        The context this is not referencing to the object missile, further, I can see you're filling an array with the same object multiple times, this means all indexes in the array are pointing to the same object which relies on errors.



        You should instantiate a new object every time the user presses UP.



        For example






        var Missile = function() {
        this.leftPos = 0;
        this.bottomPos = 0;
        this.image = { // Example
        style: {
        left: 4,
        bottom: 34
        }
        };
        this.id = null;
        this.launch = function() {
        setInterval(() => { // Use arrow function to get access to Missile lexical context.
        this.image.style.left = this.leftPos + 'px';
        this.image.style.bottom = this.bottomPos + 'px';
        this.leftPos += 0.5;
        this.bottomPos += 1;
        console.log(this.image.style.left);
        }, 500);
        };
        };

        new Missile().launch();

        .as-console-wrapper { max-height: 100% !important; top: 0; }








        var Missile = function() {
        this.leftPos = 0;
        this.bottomPos = 0;
        this.image = { // Example
        style: {
        left: 4,
        bottom: 34
        }
        };
        this.id = null;
        this.launch = function() {
        setInterval(() => { // Use arrow function to get access to Missile lexical context.
        this.image.style.left = this.leftPos + 'px';
        this.image.style.bottom = this.bottomPos + 'px';
        this.leftPos += 0.5;
        this.bottomPos += 1;
        console.log(this.image.style.left);
        }, 500);
        };
        };

        new Missile().launch();

        .as-console-wrapper { max-height: 100% !important; top: 0; }





        var Missile = function() {
        this.leftPos = 0;
        this.bottomPos = 0;
        this.image = { // Example
        style: {
        left: 4,
        bottom: 34
        }
        };
        this.id = null;
        this.launch = function() {
        setInterval(() => { // Use arrow function to get access to Missile lexical context.
        this.image.style.left = this.leftPos + 'px';
        this.image.style.bottom = this.bottomPos + 'px';
        this.leftPos += 0.5;
        this.bottomPos += 1;
        console.log(this.image.style.left);
        }, 500);
        };
        };

        new Missile().launch();

        .as-console-wrapper { max-height: 100% !important; top: 0; }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 2:15









        EleEle

        23.3k42149




        23.3k42149

















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'