gpt4 book ai didi

javascript - AngularJS。执行长时间运行的任务

转载 作者:行者123 更新时间:2023-11-28 19:29:36 26 4
gpt4 key购买 nike

AngularJS 是否有某种机制来执行长时间运行的任务?例如GWT有这样的功能:

Sometimes you want to break up your logic loop so that the JavaScript event loop gets a chance to run between two pieces of code. The Scheduler class in GWT will allow you to do that. The logic that you pass to Scheduler will run at some point in the future, after control has been returned to the JavaScript event loop. This little delay may give the interface a chance to process some user events or initialize other code.

最佳答案

在 JavaScript 中,您可以通过强制异步执行代码来实现此目的:

setTimeout(function () {
// ... code executed at some later point in time
}, 0); // timeout of `0` means it will get executed as soon as possible

在 AngularJS 中,您可以使用 $timeout 服务来完成此操作,然后可以在单元测试中轻松模拟该服务。

<小时/>

给你一个完整的例子:

var maxIterations = 100000;
(function brokenUpFn (i) {
if (i >= maxIterations) {
return;
}

do {
// ... (some computation-intensive code here)
i++;
} while (i % 100);

setTimeout(brokenUpFn, 0, i);
})(0);

关于javascript - AngularJS。执行长时间运行的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com