在大多数编程语言中,可以使用以下代码等待两秒钟:

  1. Python:
import time

time.sleep(2)
  1. Java:
try {
    Thread.sleep(2000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
  1. JavaScript:
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function waitTwoSeconds() {
    await sleep(2000);
}

waitTwoSeconds();
  1. C++:
#include <iostream>
#include <chrono>
#include <thread>

int main() {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return 0;
}

这些代码片段中的sleep函数或sleep_for函数将程序暂停执行指定的时间(以毫秒或秒为单位),从而实现等待两秒的效果。


原文地址: https://www.cveoy.top/t/topic/pJes 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录