javascript - 在弹出确认对话框之前做一些事情
问题描述
$('.lorem').on('click', function(){
$(this).hide();
if(prompt('DO SOMETHING') != null) {console.log('something');}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='lorem'>lorem</div>
所以我首先想隐藏 div 然后弹出确认对话框。可能吗?
解决方案
在下一次绘制之前使用动画帧运行代码。
window.requestAnimationFrame()
$('.lorem').on('click', function(){
// The browser will paint async not sync, so the div may still be visible
// even after this line
$(this).hide();
// when the browser is ready to paint the div off screen the callback will fire
window.requestAnimationFrame(() => {
if (prompt('DO SOMETHING') != null) {
console.log('something');
}
});
});
注意:您可能需要嵌套动画帧,因为浏览器倾向于以不同的方式实现请求动画帧。
requestAnimationFrame(() => requestAnimationFrame(() => {
...
}));
推荐阅读
- node.js - 当MongoDB中至少需要一个子文档时,有什么更好的方法来防止删除所有子文档
- java - 获取最大尺寸的连续子阵列尺寸
- c - 为什么我的 flex 和 bison 程序不起作用?正弦和余弦函数计算器
- go - image.Decode 在解码 .png 文件时返回错误
- javascript - 如果月份无效,则启用/禁用按钮
- python - scipy.integrate.solve_ivp 在 MATLAB 中完成的状态空间模拟上发散
- swift - Swift 5 - 传递给不带参数的调用的参数
- asp.net-core - .NET Core 中的 IFormFile vs byte[] 和文件上传最佳实践
- html - Pug span 无法在 flex div 中正确渲染
- typescript - rxjs 订阅一个类