23_同步异步.html 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // js 是单线程的 分为 同步任务 异步任务
  11. // 同步任务 按照顺序执行 必须等待上一个任务执行完毕 才能执行下一个任务
  12. // 异步任务 不按照顺序执行 不会阻塞后续任务的执行 会进入到任务队列中 等待同步任务执行完毕后 再执行异步任务
  13. // console.log(3);
  14. // setTimeout(function(){
  15. // console.log(1);
  16. // },1000)
  17. // console.log(2);
  18. // js执行过程中无论异步代码需要等多久都会先执行同步任务
  19. // 同步任务执行完毕后 才会执行异步任务
  20. // 常见的异步方法:
  21. // 定时函数 setTimeout setInterval
  22. console.log(3);
  23. setTimeout(function(){
  24. console.log(1);
  25. },0);
  26. console.log(2);
  27. </script>
  28. </body>
  29. </html>