如何延迟Java中的setInterval? [英] How to delay setInterval in Javascript?

查看:254
本文介绍了如何延迟Java中的setInterval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在JavaScript中反复遇到一个奇怪的问题。我似乎无法延迟 setInterval 更长的时间。

I've been running into a weird problem repeatedly in JavaScript now. I can't seem to delay setInterval longer.

发生的情况的一个小例子:

A small example of what happens:

var loop;
var count;
loop = setInterval(start, 30);

function start() {
    //Some code

    delay();

    //Some more code
}

function delay() {
    setTimeout(function () {
        count++;

        //Animation code

        if (count <= 1000) delay();
    }, 100);
}

现在我要做的是,执行某些代码部分,执行在执行动画代码时没有执行任何操作,然后在执行更多代码部分。

Now what I want to do is, execute 'some code' part, do nothing while the 'animation code' executes, and then execute 'some more code' part.

正在发生的事情是更多代码部分和延迟功能正在同时执行。我已经尝试过 clearInterval(loop),然后再调用delay函数,并在delay函数完成执行后重新启动它,但是更多代码似乎总是被执行一次。

What is happening is that the 'some more code' part and the delay function is getting executed simultaneously. I have tried clearInterval(loop) before calling the delay function and restarting it after the delay function completes it's execution, but the 'some more code' seems to get executed once anyways.

如何解决这个问题?

推荐答案

您的 delay()函数实际上并不会阻止您的 start()函数完成。

Your delay() function does not actually stop your start() function from completing.

在JavaScript中, setTimeout()不会暂停Javascript的执行。相反,它的工作是安排传递给 setTimeout()的回调在将来某个时间运行,而其余代码将继续运行。

In Javascript, setTimeout() does not pause the execution of Javascript. Instead, what it does is schedule the callback passed to setTimeout() to run sometime in the future and the rest of your code just keeps on running.

如果要在一段时间内延迟执行某些代码块,则必须将该代码块放入 setTimeout()这样的回调:

If you want to delay the execution of some block of code for some time period, then you have to put that block of code inside the setTimeout() callback like this:

setTimeout(function() {
    // the code in here will run some time in the future
}, delayTime);

// the code here will run immediately

这种方式通常Javascript中的描述是说对 setTimeout()的调用是非阻塞的。它不会停止或暂停当前执行线程的运行。实际上,执行线程将运行完毕。相反, setTimeout()安排在将来的某个特定时间(当没有其他Javascript代码运行时)调用回调函数。

The way this is often described in Javascript is to say that a call to setTimeout() is non-blocking. It doesn't stop or pause the running of the current thread of execution. In fact that thread of execution will run to completion. Instead, setTimeout() schedules a callback function to be called at a particular time in the future (when no other Javascript code is running).

因为Javascript是单线程的,所以即使您使用了很长的 while()循环。问题是,在循环时,没有其他Javascript代码可以运行,因此其他任何状态都不能更改,因此无论您要更改的循环条件如何,在循环时都不会改变。这通常只会导致死锁类型的情况,最终浏览器意识到Javascript线程卡住了,并提示中止它。

Because Javascript is single-threaded, it pretty much never works to loop to try to create a delay, even if you use a really long while() loop. The issue is that while you are looping, no other Javascript code can run so no other state can change so whatever loop condition you are waiting to change will not change while you are looping. This commonly just leads to a deadlock type of condition and eventually the browser realizes that the Javascript thread is "stuck" and will prompt to abort it.

相反,您可以使用事件和将来的回调。您设置了一个事件或计时器回调,以便在将来发生某事时可以调用它,然后将相关代码放入该回调或事件处理程序中。还有更多高级工具可以帮助管理此问题,例如Promise或异步库,但它们只是建立在相同的回调机制之上。

Instead, you program with events and future callbacks. You set up an event or timer callback so it can be called at some future time when something happens and then you put the relevant code in that callback or event handler. There are also more advanced tools to help manage this such as promises or async libraries, but they're just built on top of the same callback mechanism.

这篇关于如何延迟Java中的setInterval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆