当前位置:网站首页>Attention should be paid to the first parameter of setTimeout
Attention should be paid to the first parameter of setTimeout
2022-07-27 03:10:00 【Wuuconix】
setTimeout The first parameter should pay attention to
The first parameter should be a function , Instead of a function call .
setTimeout(console.log('2333'), 1000)
Such a statement seems to have no problem ,1000 In milliseconds console.log(‘23333’). But it's totally wrong , In the browser you will find , He didn't wait 1000 millisecond , And directly output .
And in the node Will be output immediately 23333 Report a mistake later .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-PRFJwWj8-1657604281201)(http://qiniu.wuuconix.link/image-20220712132906968.png)]](/img/58/0b7bc7f04be0276f92cd05fbfd12d8.png)
The error message is Callback must be a function. Received undefined. We know ,console.log The return value of undefined.
therefore console.log('23333') It looks like a function here , And he is actually a undefined.
So we can have two solutions , One is to pass in parameters later .
setTimeout(console.log, 1000, '23333')
This is troublesome when there are many parameters , It's not easy to understand .
A better way is to wrap the call statement in an arrow function .
setTimeout(() => console.log('23333'), 1000)
This is beautiful and easy to understand .
边栏推荐
猜你喜欢

Arduinouno drive RGB module full color effect example

What did kubedmin do?

CS224W fall 1.2 Applications of Graph ML

185. 部门工资前三高的所有员工(必会)

Concept of data asset management

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"

iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》

CAS部署使用以及登录成功跳转地址

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
随机推荐
Static keyword
一体式水利视频监控站 遥测终端视频图像水位水质水量流速监测
idea中常用的快捷键
How to use devaxpress WPF to create the first MVVM application in winui?
Shortcut keys commonly used in idea
全网最全的软件测试基础知识整理(新手入门必学)
一道数学题,让芯片巨头亏了5亿美金!
Arduinouno drive RGB module full color effect example
基于GoLang实现API短信网关
MarqueeView实现滑动展示效果
阿里云技术专家杨泽强:弹性计算云上可观测能力的构建
My crawler notes (VII) blog traffic +1 through Crawlers
HCIP第十四天笔记
Database read-write separation and database and table segmentation
Make ppt timeline
2513: Xiao Yong's academic score (common divisor problem)
Integrated water conservancy video monitoring station telemetry terminal video image water level water quality water quantity flow velocity monitoring
CS224W fall 课程 ---- 1.1 why Graphs ?
[simple question of stack and queue] leetcode 232. realize queue with stack, 225. realize stack with queue
智能指针shared_ptr、unique_ptr、weak_ptr