了解与setTimeout和setInterval一起使用的javascript承诺 [英] Understanding javascript promise used with setTimeout and setInterval

查看:214
本文介绍了了解与setTimeout和setInterval一起使用的javascript承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解这两行代码并改进它们以实际允许我停止重复吗?

Can someone help me understand these 2 lines of code and improve them to actually allow me to stop the repeats?

var wait = ms => new Promise(r => setTimeout(r, ms));
var repeat = (ms, func) => new Promise(r => (setInterval(func, ms), wait(ms).then(r)));

repeat(1000, () => Promise.all([myfunction()])
  .then(...)


推荐答案

第一行(等待)只是等待一定的毫秒数然后结束。

The first line (wait) simply waits a certain number of milliseconds and then ends.

第二行(repeat)编程一个函数在一定的时间间隔内运行(setInterval),然后调用wait并将重复函数设置的毫秒数传递给它。这些函数只调用一次.Javascript的setInterval内部控件是从现在起控制()=> Promise.all([myfunction()])在编程时间控制的间隔。

The second line (repeat) programs a function to be run in a certain interval of time (setInterval), and then calls wait and passes the number of milliseconds set on repeat function to it. These function are called only once. Javascript's internal controls for setInterval is what takes control from now on calling () => Promise.all([myfunction()]) at the programmed time interval.

如果您只是识别您的代码,事情会更清楚。

If you just ident your code things get clearer.

var wait = 
    ms => new Promise(
        r => setTimeout(r, ms)
    );

var repeat = 
    (ms, func) => new Promise(
        r => (
            setInterval(func, ms), 
            wait(ms).then(r)
        )
    );

repeat(1000, () => Promise.all([myfunction()]))
.then(...);

为了停止该函数,你必须捕获间隔的id并按SimpleJ指出调用clearInterval。
一旦你全身心投入,你可能会想要使用Promises。所以一个完整的工作示例是:

In order to stop the function you have to capture the interval's id and call clearInterval as SimpleJ pointed out. You probably will want to do this with Promises once you're all into it. So a complete working example would be:

var intervalID = 0;

var wait = 
    ms => new Promise(
        r => setTimeout(r, ms)
    );

var repeat = 
    (ms, func) => new Promise(
        r => (
            intervalID = setInterval(func, ms), 
            wait(ms).then(r)
        )
    );

var myfunction = 
    () => new Promise(
        r => r(console.log('repeating...'))
    );

var stopAfter5Secs = 
    () => new Promise(
        r => r(setTimeout(() => { 
                    clearInterval(intervalID);
                    console.log('repeat end') 
               } , 5000))
    );

repeat(1000, () => Promise.all([myfunction()])) // 1000 miliseconds = 1 second
.then(stopAfter5Secs())  // starts timer to end repetitions
.then(console.log('repeat start')); // informs that all actions were started correctly and we are waiting for them to finish

Promiss.all调用传递给它的任何可交换对象中的所有promisses。在这种情况下,只有一个元素(myfunction)的数组。我创建了一个简单的函数,它只将'重复'写入控制台,成为该函数。但是如果所有函数都返回promisses,你可以传递任意数量的函数。

Promiss.all calls all promisses in any interable object passed to it. In this case an array with only one element (myfunction). I created a simple function which only writes 'repeating' to the console, to be that function. But you can pass any number of functions you want to it if all of them return promisses.

你可以看到它在这里工作: https://jsfiddle.net/9n2knxdg/7/

You can see it working here: https://jsfiddle.net/9n2knxdg/7/

这篇关于了解与setTimeout和setInterval一起使用的javascript承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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