出于对是否会异步启动setTimeout()的好奇,我尝试了以下测试脚本:
function timedText() {
var x = document.getElementById("txt");
setTimeout(function() {
x.value = "1 second"
}, 1000);
setTimeout(function() {
x.value = "2 seconds"
}, 2000);
setTimeout(function() {
x.value = "3 seconds"
}, 3000);
while (true) {}
}<p>Click on the button below. The input field will tell you when two, four, and six seconds have passed.</p>
<button onclick="timedText()">Display timed text</button>
<input type="text" id="txt">
当然,单击该按钮会导致浏览器挂起。
这告诉我,setTimeout()在单独的线程上运行而不是。
但在最近的一次采访中,面试官提出了相反的建议.这是否意味着setTimeout()依赖于浏览器/实现?
发布于 2018-08-22 06:32:00
javascript中没有线程。setTimeout只将委托函数推送到下一次将弹出的堆栈中。你可以读到那个JavaScript和线程
https://stackoverflow.com/questions/51960879
复制相似问题