我正在尝试使用window.requestAnimationFrame()时保持时间,但是+ =运算符始终返回NaN,即使我要增加的值不是NaN
var elapsedTime = 0
var lastTime = 0
var between = 500
var count = 0
var betweenCount = 0
function frame(time) {
elapsedTime = time - lastTime
console.log(elapsedTime)
lastTime = time
betweenCount += elapsedTime
console.log(betweenCount)
window.requestAnimationFrame(frame)
}
frame()
betweenCount应该跟踪自上次重置以来的总经过时间,但是一旦为其分配了经过时间,它就变为NaN,但此时的经过时间不是NaN。
答案 0 :(得分:5)
由于您是第一次调用frame()
,而没有任何参数,因此time
将在函数中为undefined
,elapsedTime
将变为undefined - lastTime
,即NaN
。
您需要给初始的frame()
调用一个有效的参数,例如0
。
答案 1 :(得分:0)
只是因为您在调用函数时未传递任何参数值,所以当时是void Foo::CloseTable()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
{
std::unique_lock<std::mutex> lock{ mutex_open_table };
std::cout << "Close Table: " << my_number << std::endl;
Sleep(1000);
notified_close_table = true;
}
close_table_condition_variable.notify_one();
}
}
,而您尝试使用undefined
来升级值,+ operator
适用于数字,在您的情况下,您尝试增加一些值,因此它首先尝试将字符串转换为数字,然后突然抛出错误+ operator
;
NaN