javascript - 为什么此 JS 代码中的光标不更新?
问题描述
我正在编写 javascript 代码,我得出的结论是,在 CPU 密集型代码之前执行时,更改 html 页面的任何/所有元素上的光标不会立即受到影响。即使移动我的鼠标,代码运行时也没有任何变化。
例如,此代码仅在 for 循环完成后更改光标:
$('*').css('cursor', 'wait'); for (let i=0; i<10000000000; i++) ;
如果我运行以下命令,用户甚至不会在循环期间看到任何变化:
$('*').css('cursor', 'wait'); for (let i=0; i<10000000000; i++) ; $('*').css('cursor', '');
任何其他 css 更改(背景颜色、元素大小等)都可以正常工作,但光标不能...
无论如何要解决这个问题?
(在 Chrome 和 Firefox 上测试过)
解决方案
我们可以:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
html {
height: 100%;
}
body {
height: 100%;
}
</style>
</head>
<body>
<script>
document.body.style.cursor = 'wait'
getData()
function getData() {
for (let index = 0; index < 20; index++) {
console.log(index)
}
endWork()
}
function endWork() {
setTimeout(() => {
document.body.style.cursor = 'default'
alert('change cursor to default')
}, 3000);
}
</script>
</body>
</html>
推荐阅读
- mongodb - 从js文件执行查询时mongodb不要求输入密码
- php - Shopify Webhook JSON 到 PHP 的使用和发布变量
- ios - 为 iOS 应用程序定义 SSL 自定义 URL 方案
- javascript - 如何在解构与抛出错误中返回 null
- javascript - 如何从 span 中获取每个值并使用 js 或 jquery 将所有值设置为变量?
- python-3.x - 在从独立 pyinstaller exe 中存在的 tkinter 启动的命令提示符下运行 python 函数
- javascript - 是否可以在 JavaScript 调试器中查看对象的构造位置?
- python - 如何使用 Python 将文本内容旁边的多个图像发送到 Discord 频道?
- javascript - 无法让我的 if 语句在“app.post”函数中工作
- arrays - 为什么我不能从文本文件中连接重建和数字数组 c char 的内容?