java实现:
1 public class Sleep {
2 public static void main(String args[]) {
3 try {
4 System.out.println(new Date( ) + "\n");
5 Thread.sleep(1000*5); // 休眠5秒
6 System.out.println(new Date( ) + "\n");
7 } catch (Exception e) {
8 System.out.println("exception!");
9 }
10 }
11 }
js实现:
1 //参数n为休眠时间,单位为毫秒:
2 function sleep(n) {
3 let start = new Date().getTime();
4 console.log('休眠前:' + start);
5 while (true) {
6 if (new Date().getTime() - start > n) {
7 break;
8 }
9 }
10 console.log('休眠后:' + new Date().getTime());
11 }
12
13 sleep(2000);