+ 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
7 Respuestas
+ 3
Théophile but why doesn't timechange return NaN yet it's also dependent on the timestamp parameter?
+ 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 ^^
+ 3
visph that works. It also works when I supply time_0 as the argument when I call update()
+ 2
How timestamp is initialized ?
I don;t know js but what about try inside the function
timestamp = Date.now();
+ 2
Your update function takes one parameter : if you give None, 0+none=Nan
+ 1
Your function update returns undefined because it's looking for a function update without parameter...
+ 1
I don't know why it returns undefined and not Nan...