我想延迟使用functin,首先在调用函数时设置lastTime。然后,如果没有经过10秒,不要让该函数再次运行。
void SetUseFunctionLastTime()
{
std::chrono::system_clock::now();
}
bool UseFunction()
{
if (/*Code here*/) /*HERE i want to measure make a check that allow me to use this function only each 10 seconds*/
{
Log("Wait %d seconds to use this", seconds_left)
return false;
}
SetUseFunctionLastTime()
}
该怎么做?
使用-
运算符计算时差:
void SetUseFunctionLastTime()
{
last_time = std::chrono::system_clock::now();
}
bool UseFunction()
{
std::chrono::duration<double> diff = std::chrono::system_clock::now() - last_time;
auto seconds_left = diff.count();
if (seconds_left < 10)
{
Log("Wait %d seconds to use this", seconds_left);
return false;
}
SetUseFunctionLastTime();
}