gpt4 book ai didi

javascript - 单击时终止所有正在运行的 JavaScript 函数

转载 作者:行者123 更新时间:2023-11-29 23:22:45 24 4
gpt4 key购买 nike

这是我之前问题的延伸,主要是出于好奇:Kill all running JavaScript functions with Timeouts on click

我有一个包含 3 个按钮和一个“结果”部分的网页:

<button id="b1" onclick="f1()">1st button</button>
<button id="b2" onclick="f2()">2nd button</button>
<button id="b3" onclick="f3()">3rd button</button>
<p id="result">"Here will be the result!"</p>

以下是 JavaScript 函数的片段。我还利用了网络 worker :

// The functions are similar, but the outputs are different

var worker;

function calcF1() {
var answer;
for (let i = 0; i < 1e9; i++) {
// Do some complicated calculations
}
postMessage(answer);
}

function f1() {
// TODO: Kill other running instances of all functions

// Calling worker
var cf1 = calcF1.toString();
var code = cf1.substring(cf1.indexOf('{')+1, cf1.lastIndexOf('}'));
var blob = new Blob([code], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob));

worker.onmessage = message => {
document.getElementById("result").innerHTML = "1st function";
};
}

function calcF2() {
var answer;
for (let i = 0; i < 1e9; i++) {
// Do some complicated calculations
}
postMessage(answer);
}

function f2() {
// TODO: Kill other running instances of all functions

// Calling worker
var cf2 = calcF2.toString();
var code = cf2.substring(cf2.indexOf('{')+1, cf2.lastIndexOf('}'));
var blob = new Blob([code], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob));

worker.onmessage = message => {
document.getElementById("result").innerHTML = "2nd function";
};
}

function calcF3() {
var answer;
for (let i = 0; i < 1e9; i++) {
// Do some complicated calculations
}
postMessage(answer);
}

function f3() {
// TODO: Kill other running instances of all functions

// Calling worker
var cf3 = calcF3.toString();
var code = cf3.substring(cf3.indexOf('{')+1, cf3.lastIndexOf('}'));
var blob = new Blob([code], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob));

worker.onmessage = message => {
document.getElementById("result").innerHTML = "3rd function";
};
}

但是,如果用户单击b1,然后立即单击b2,则f1 的worker 不会终止并继续运行。如果来自 f1 的 worker 在 f2 完成之前完成,则 result 将显示 1st function 然后替换为 第二个函数f2 的 worker 也有可能先完成,在这种情况下 result 将显示 2nd function 然后被 1st 替换函数

我想要的是在每个函数的开始处实现一个语句,该语句会终止/忽略/绕过所有函数和/或当前正在运行的所有网络 worker ,以便最终输出始终是与用户按下的最后一个按钮。如何最好地实现它?

编辑:感谢您指出网络 worker 。我最初的实现涉及网络 worker ,但我忘了提及。问题已更新。

最佳答案

拆分函数,使它们在停止之前只运行一段适中的时间,并使用 setTimeout 将下一次运行推到堆栈的末尾,以便其他函数有机会运行.在每个f1f2f3的开头,检查它们是否具有对当前运行函数的键的唯一引用, 如果他们不这样做则返回。

在以下代码片段中,正在进行的 f1 函数被最近的 f2 调用覆盖:

let runningFunctionKey;
function f1(key, start = 0) {
if (!key) {
runningFunctionKey = {}; // some unique reference
key = runningFunctionKey;
} else if (key !== runningFunctionKey) return;
const until = start + 1e7;
while (start < until) {
if (start === 1e9) {
console.log('f1 done');
return;
}
start++;
}
console.log('f1 setting timeout');
setTimeout(f1, 0, key, start);
}
function f2(key, start = 0) {
if (!key) {
runningFunctionKey = {}; // some unique reference
key = runningFunctionKey;
} else if (key !== runningFunctionKey) return;
const until = start + 1e7;
while (start < until) {
if (start === 1e9) {
console.log('f2 done');
return;
}
start++;
}
console.log('f2 setting timeout');
setTimeout(f2, 0, key, start);
}
f1();
setTimeout(f2, 500);

关于javascript - 单击时终止所有正在运行的 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146270/

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