javascript - 如何在不浪费计算的情况下暂停 javascript 方法?
问题描述
本质上,您如何暂停 javascript 执行,而不必将计算浪费在诸如 while 循环之类的东西上?
例如,假设我只想在 10 秒后执行一个功能,而不中断其他进程或浪费计算?setTimeout
将不起作用,因为我希望进程在此期间实际暂停/不继续任何操作。
const next = () => console.log("next");
/** pause for 10 seconds */
next()
另外,如果我只想有条件地运行一个方法,每隔几秒钟或某事我可以中止操作或继续。值得注意的是,我并不是要使用setInterval
,因为在这种情况下,它实际上并没有有条件地暂停实际的 javascript 执行。
const next = () => console.log("next");
const start = new Date();
const startSeconds = now.getSeconds();
const checkEvery = 1; // seconds
const requiredDiff = 10; // seconds
const checker = () => {
const now = new Date();
let secondsNow = now.getSeconds();
secondsNow < startSeconds ? secondsNow += 60 : null;
const diff = secondsNow - startSeconds;
if (diff < 10) {
return false
}
return true;
}
/** something that runs checker every 1 seconds and pauses computation until checker() returns true and then runs next() */
解决方案
一个很好的方法是在管道中创建一个块。IE
process -> pause -> next
这样您就可以知道在满足特定条件之前程序的流程不会继续
你可以通过暂停内联的东西来做到这一点
...
await pause();
...
在哪里pause
做超时承诺或类似的事情。这个方法我不是很了解,但是看起来比较复杂。
另一种方法是使用 while 循环停止内联执行
...
// pause for 10 seconds
let result;
while (!result) { result = checker() };
...
但是正如您所提到的,这会浪费大量操作,并且会干扰后台其他操作的正常运行。另一件事是您不能仅每 1 秒检查一次检查器。
我建议您改为执行以下操作:
const max = 20; // max number of recursive calls (i.e. timeout after 20 seconds)
// checker is a function that returns true or false and is agnostic to this implementation
// timeout is the time (in milliseconds) to wait before running the checker again
// next is the next step in your pipeline that you want to prevent from e
const pause = async (checker, timeout, next, calls = 0) => {
if (calls > max) return; // prevents stack overflow
const result = await checker(); // just in case your checker is async
// if the condition was met then continue on to the next stage in your pipeline
// if the condition was not met then run this method again to re-check in timeout
result ? next() : setTimeout(() => pause(checker, timeout, next, calls + 1), timeout)
}
// with the functions you provided...
pause(checker, 1000, next)
pause
只有在满足超时时才会执行操作,并且在满足检查器之前不允许程序继续下一个阶段。
推荐阅读
- bash - 将日期/时间值传递给 bash 函数中的触摸命令
- javascript - 如何保存 blob 并使用 wavesurfer 播放
- python - 数据框将现有列值更改为单个特定值
- azure-cognitive-search - Azure 认知搜索 - 通过 API 创建数据源
- sql - 将 varchar 值 ' 和 f.FUNCTION_ID= CAST(' 转换为数据类型 int 时转换失败
- javascript - 如何在 JavaScript 代码中添加 HTML 按钮?
- javascript - 反应打开文件对话框
- javascript - NodeJS Mocha 不会将参数传递给函数调用
- javascript - 在 GET 上发送嵌套对象
- swift - 当从一个视图控制器转到另一个视图控制器(如 Swift 中的 Instagram 或 Facebook)时,如何在表视图中保留当前的 IndexPath.row?