VS2017中使用C++語言編寫delay函數實現延遲


秒級別的延時

//定義函數
void delay(int sec){
    time_t start_time, cur_time; // 變量聲明
    time(&start_time);
    do {
     time(&cur_time);} 
    while ((cur_time - start_time) < sec);
}
//調用
delay(5); //滯后5秒

毫秒級別的延時

clock_t start_time, cur_time;
start_time = clock();
while((clock() - start_time) < 3.0 * CLOCKS_PER_SEC)
{
}
//3.0為毫秒參數
//有的編譯器不支持clock
//推薦MS VC++ MFC的Sleep(毫秒)函數

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM