调用函数 n 次,间隔为 n 秒

Call a function n times with an interval of n seconds

本文关键字:函数 调用      更新时间:2023-10-16

我想在 10 秒的间隔内简单地调用一个函数 4 次。这只调用它一次

    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    connect(timer, SIGNAL(timeout()), this, SLOT(myFunction()));
    timer->start(10000);

如果我摆脱了setSingleShot方法,它将永远被调用。有没有一种内置的方式只调用它n次。我在Qt文档中找不到它。

蛮力、不可扩展的方式:启动四个计时器。

更实用的方法是让回调跟踪它被调用的次数,然后在第四次调用时停止计时器。