js setTimeout和setInterval区别
生活随笔
收集整理的這篇文章主要介紹了
js setTimeout和setInterval区别
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、區(qū)別
2、示例代碼
<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta http-equiv="X-UA-Compatible" content="ie=edge" /><title>js setTimeout和setInterval區(qū)別</title></head><body><script type="text/javascript">let scriptBegin = Date.now();//fun1(); fun2();//需要執(zhí)行20ms的程序function act(functionName) {console.log(functionName, Date.now() - scriptBegin);let begin = Date.now();while(Date.now() - begin < 20);}// function fun1 () {// let fun3 = ()=>{act('fun3')}// setTimeout(fun3,20);// act('fun1')// }function fun2() {act('fun2-1')let fun4 = () => {act('fun4')}setInterval(fun4, 20);act('fun2-2')}</script></body></html>注:可以分別執(zhí)行fun1和fun2查看執(zhí)行順序。
更多專業(yè)前端知識(shí),請(qǐng)上 【猿2048】www.mk2048.com 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的js setTimeout和setInterval区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: http 请求报文和响应报文
- 下一篇: flow 静态类型检查 js