C++实现简单的定时器,即每隔一段时间执行一遍代码
#include <iostream> #include <ctime> using namespace std; int getTime() { return clock()/CLOCKS_PER_SEC; } int main() { int i = 0; int lastTime = 0; while (1) { int now = getTime(); if (now - lastTime > 0) { cout << ++i << endl; lastTime = now; } } return 0; }