javascript功能_JavaScript睡眠功能

javascript功能

The infamous sleep, or delay, function within any language is much debated.  Some will say that there should always be a signal or callback to fire a given functionality, others will argue that sometimes an arbitrary moment of delay is useful.  I say that to each their own and one rule can never dictate anything in this industry.

臭名昭著的sleepdelay功能在任何语言中都存在很多争议。 有些人会说应该总是有一个信号或回调来触发给定的功能,另一些人则认为有时任意的延迟时间是有用的。 我说,在这个行业中,对每个人来说,一条规则永远不能决定任何事情。

Writing a sleep function is simple and made even more usable with JavaScript Promises:

编写sleep函数非常简单,并且可以通过JavaScript Promises进一步使用:


// https://zeit.co/blog/async-and-await
function sleep (time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

// Usage!
sleep(500).then(() => {
    // Do something after the sleep!
})


Without promises you'd need to pass a callback; with our beautiful promises we simply resolve after the setTimeout and use then with the result to execute the next step.  You'll also note that the demo above uses ES6 arrow functions.

没有承诺,您将需要传递回调; 有了我们美好的承诺,我们只需在setTimeout之后解决, then其与结果一起使用即可执行下一步。 您还将注意到,上面的演示使用了ES6箭头功能

翻译自: https://davidwalsh.name/javascript-sleep-function

javascript功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值