123456789101112131415161718192021222324252627282930313233343536373839 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <!--
- 1、定时器真是定时执行的吗?
- 定时器并不能保证真正定时执行
- 一般会延迟一丁点儿(可以接受),也有可能延迟很长时间(不能接受)
- 2、定时器回调函数是在分线程执行的吗?
- 在主线程执行的,js是单线程的
- 3、定时器是如何实现的?
- 事件循环模型
- -->
- <button id="btn">启动定时器</button>
- <script>
- document.getElementById('btn').onclick = function () {
- // var start = new Date().getTime()//写成下面的语句更简便
- var start = Date.now()
- console.log('启动定时器前')
- setTimeout(function () {
- console.log('定时器执行了', Date.now() - start)
- }, 200)
- console.log('启动定时器后')
- //做一个长时间的工作
- for(var i=0;i<1000000000;i++){
-
- }
- }
- </script>
- </body>
- </html>
|