这是我之前提问的延伸,主要是出于好奇: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函数的代码段。我还利用了网络工作者:
// 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
旁边的工作人员将不会终止并继续运行。如果来自f1
的工作人员在f2
之前完成,则result
将显示1st function
,然后由2nd function
替换。 f2
之前的工作人员也可能先完成,在这种情况下result
会显示2nd function
,然后由1st function
替换。
我想要的是在每个函数的开头实现一个语句,该语句杀死/忽略/绕过当前正在运行的所有函数和/或所有Web工作者,以便最终输出始终是与用户按下的最后一个按钮。如何才能最好地实施?
编辑:感谢您指出网络工作者。我最初的实现涉及网络工作者,但我忘了提及。问题已经更新。
答案 0 :(得分:1)
拆分这些函数,使它们只运行一段时间,然后停止并使用setTimeout
将下一次运行推送到堆栈的末尾,以便其他函数有机会运行。在每个f1
,f2
和f3
的开头,检查他们是否具有对当前运行功能的键的唯一引用,如果他们不在,则返回。吨。
在以下代码段中,正在进行的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);
&#13;