[SOLVED] NaN error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

[SOLVED] NaN error

In this code, why does 'inc' return Not a Number, yet 'deltatime' doesn't? All I did was add deltatime to inc! 😢 var time_0=0; var inc=0; var deltatime; function update(timestamp){ deltatime=timestamp-time_0; inc+=deltatime; console.log(inc,deltatime); time_0=timestamp; window.webkitRequestAnimationFrame(update); } update(); Try it yourself https://code.sololearn.com/WTF3c76tSa6s/#html

23rd Nov 2018, 10:28 AM
Moses Odhiambo
Moses Odhiambo - avatar
7 Answers
+ 3
Théophile but why doesn't timechange return NaN yet it's also dependent on the timestamp parameter?
23rd Nov 2018, 11:04 AM
Moses Odhiambo
Moses Odhiambo - avatar
+ 3
Do your first call to your 'update' function through a requestAnimationFrame call: var time_0=0; var inc=0; var deltatime; function update(timestamp){ deltatime=timestamp-time_0; inc+=deltatime; console.log(inc,deltatime); time_0=timestamp; window.webkitRequestAnimationFrame(update); } window.webkitRequestAnimationFrame(update); Anyway, webkitRequestAnimationFrame is browser vendor prefixed and should only work in webkit based browsers ^^
23rd Nov 2018, 4:18 PM
visph
visph - avatar
+ 3
visph that works. It also works when I supply time_0 as the argument when I call update()
24th Nov 2018, 4:27 AM
Moses Odhiambo
Moses Odhiambo - avatar
+ 2
How timestamp is initialized ? I don;t know js but what about try inside the function timestamp = Date.now();
23rd Nov 2018, 10:59 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 2
Your update function takes one parameter : if you give None, 0+none=Nan
23rd Nov 2018, 11:00 AM
Théophile
Théophile - avatar
+ 1
Your function update returns undefined because it's looking for a function update without parameter...
23rd Nov 2018, 11:10 AM
Théophile
Théophile - avatar
+ 1
I don't know why it returns undefined and not Nan...
23rd Nov 2018, 11:11 AM
Théophile
Théophile - avatar