1. <i id='tEZ8J'><tr id='tEZ8J'><dt id='tEZ8J'><q id='tEZ8J'><span id='tEZ8J'><b id='tEZ8J'><form id='tEZ8J'><ins id='tEZ8J'></ins><ul id='tEZ8J'></ul><sub id='tEZ8J'></sub></form><legend id='tEZ8J'></legend><bdo id='tEZ8J'><pre id='tEZ8J'><center id='tEZ8J'></center></pre></bdo></b><th id='tEZ8J'></th></span></q></dt></tr></i><div id='tEZ8J'><tfoot id='tEZ8J'></tfoot><dl id='tEZ8J'><fieldset id='tEZ8J'></fieldset></dl></div>

    1. <tfoot id='tEZ8J'></tfoot>

      <small id='tEZ8J'></small><noframes id='tEZ8J'>

        <bdo id='tEZ8J'></bdo><ul id='tEZ8J'></ul>

      <legend id='tEZ8J'><style id='tEZ8J'><dir id='tEZ8J'><q id='tEZ8J'></q></dir></style></legend>
    2. Javascript中的睡眠()

      sleep() in Javascript(Javascript中的睡眠())
      <tfoot id='lEp93'></tfoot>

          <tbody id='lEp93'></tbody>

          <small id='lEp93'></small><noframes id='lEp93'>

            <bdo id='lEp93'></bdo><ul id='lEp93'></ul>
                <legend id='lEp93'><style id='lEp93'><dir id='lEp93'><q id='lEp93'></q></dir></style></legend>
              • <i id='lEp93'><tr id='lEp93'><dt id='lEp93'><q id='lEp93'><span id='lEp93'><b id='lEp93'><form id='lEp93'><ins id='lEp93'></ins><ul id='lEp93'></ul><sub id='lEp93'></sub></form><legend id='lEp93'></legend><bdo id='lEp93'><pre id='lEp93'><center id='lEp93'></center></pre></bdo></b><th id='lEp93'></th></span></q></dt></tr></i><div id='lEp93'><tfoot id='lEp93'></tfoot><dl id='lEp93'><fieldset id='lEp93'></fieldset></dl></div>
                本文介绍了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中的睡眠()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

                    <bdo id='NKBaE'></bdo><ul id='NKBaE'></ul>
                    <tfoot id='NKBaE'></tfoot>
                    <i id='NKBaE'><tr id='NKBaE'><dt id='NKBaE'><q id='NKBaE'><span id='NKBaE'><b id='NKBaE'><form id='NKBaE'><ins id='NKBaE'></ins><ul id='NKBaE'></ul><sub id='NKBaE'></sub></form><legend id='NKBaE'></legend><bdo id='NKBaE'><pre id='NKBaE'><center id='NKBaE'></center></pre></bdo></b><th id='NKBaE'></th></span></q></dt></tr></i><div id='NKBaE'><tfoot id='NKBaE'></tfoot><dl id='NKBaE'><fieldset id='NKBaE'></fieldset></dl></div>

                    <small id='NKBaE'></small><noframes id='NKBaE'>

                      <legend id='NKBaE'><style id='NKBaE'><dir id='NKBaE'><q id='NKBaE'></q></dir></style></legend>
                            <tbody id='NKBaE'></tbody>