javascript_JavaScript waitForTime

javascript

I write a lot of tests for new features within Firefox DevTools. We have hundreds of "mochitests" which open the browser and perform synthetic actions like clicking, typing, and other user actions. I've previously written about waitForever which essentially halts following actions without locking the browser. Another utility I enjoy is waitForTime, an async JavaScript function that I can await to give breathing time between two tasks.

我为Firefox DevTools中的新功能编写了许多测试。 我们有数百个“ mochitests”,它们可以打开浏览器并执行诸如单击,键入和其他用户操作之类的综合操作。 之前,我曾写过关于waitForever ,它实际上是在不锁定浏览器的情况下停止了后续操作。 我喜欢的另一个实用程序是waitForTime ,这是一个异步JavaScript函数,可以await两个任务之间的喘息时间。

Whenever I want to wait a given amount of time between tasks, I employ this function:

每当我想在任务之间等待给定的时间时,我都会使用以下功能:

function waitForTime(ms) {
  return new Promise(r => setTimeout(r, ms));
}

/* Usage */
await waitForTime(200);
// ...do other thing...
await waitForTime(200);
// ...do next thing ...

It's important to point out that most waitForTime calls don't appear in the final test, since arbitrary timeouts lead to intermittent test failures, but they are helpful in knowing where I need to add polling for some other condition!

重要的是要指出,大多数的waitForTime调用不会出现在最终测试中,因为任意超时都会导致间歇性的测试失败,但是它们对于了解在其他情况下需要在何处添加轮询很有帮助!

翻译自: https://davidwalsh.name/waitfortime

javascript

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值