测量时间使用功能并显示它

问题描述 投票:0回答:1

我想延迟使用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()
}

该怎么做?

c++
1个回答
0
投票

使用-运算符计算时差:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.