Javascript中的睡眠()

2023-06-21前端开发问题
21

本文介绍了Javascript中的睡眠()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设由于某种奇怪的原因我想阻止 Javascript 执行一段时间,我该怎么做.JS 中没有 sleep().请不要说做一个 while() 循环,因为那很糟糕.我可以做一个 window.showModalDialog 并将一个 window.close 放在模态对话框中,setTimeout 的时间非常短,这样用户就不会注意到对话框.这就像一小段时间的睡眠,如果需要,我可以多次调用它.还有其他方法吗?

Suppose I want to block Javascript execution for certain time for some weird reason, how can I do that. There is no sleep() in JS. Pls don't say do a while() loop because that's bad. I can do a window.showModalDialog and put a window.close in the modal dialog with setTimeout of very small time so that the user doesn't notice the dialog. This will be like sleep for small time period and I can call this multiple time if needed. Is there some other way?

详细地说,我的用例是 HTML5 SQL 数据库提供了异步 api,但我想将它用于存储永远不会很大的小型 web 应用程序.所以不需要异步 api,因为小型存储上的查询将在客户端运行.所以我想写一个带有sync api的ORM,这样开发者可以更方便地使用它.为了将此异步连接到同步 api,我需要睡眠之类的东西.

To elaborate, my use case is that HTML5 SQL Database has given async api's but I want to use it for a samll webapp for which the store will never be big. So there is no need of an async api because the queries on the small store will run on the client side. So I want to write an ORM with sync api so that developers can use it more easily. To bridge this async to sync api, I need something like sleep.

推荐答案

window.setTimeoutwindow.setInterval 几乎是你唯一的朋友.

window.setTimeout or window.setInterval are pretty much your only friends.

如何使用setTimeout递归调用设置另一个超时的函数的示例如下

An example of how to use setTimeout to recursively call a function that sets another timeout is as follows

function go() {
    if (go.count < 4) {
        // logs 1, 2, 3 to firebug console at 1 second intervals
        console.log(go.count++);
        window.setTimeout(go, 1000);
    }
}
go.count = 1;

go();

您可以选择捕获 timeoutID 以用于 window.clearTimeout 如果您需要在超时完成之前清除它.

You may choose to capture the timeoutID to use with window.clearTimeout if you need to clear the timeout prior to it finishing.

请注意,window.setTimeoutwindow.setInterval 阻止执行其他脚本 - 我不这样做不相信以当前的形式使用 JavaScript 是可能的.有一些方法可以通过编码来模拟 UI 阻塞,例如使用 showModalDialog 或使用一些全局的 blocking 布尔值,这恐怕是你所能得到的.

Note that neither window.setTimeout nor window.setInterval block execution of other script - I don't believe that this is possible with JavaScript in it's current guise. There are ways that could be coded to mimic UI blocking, such as using showModalDialog or having some global blocking boolean which are about as near as you can get I'm afraid.

这篇关于Javascript中的睡眠()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui树状组件tree怎么默认勾选?
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09 前端开发问题
372

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262