本文介绍了如果执行时间太长,则跳过该功能.的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果执行时间过长,如何跳过功能.

How can I skip a function if the executing time too long.

例如,我有3个功能:

function A(){
 Do something.. ..
}

function B(){
 Do something.. ..
}

function C(){
 Do something.. ..
}

A();
B();
C();

因此,例如由于某种原因,函数B内部具有无限循环,并保持运行.如果功能B执行时间过长,如何跳过功能B并转到功能C?

So for example for some reason function B have infinity loop inside, and keep running. How can I skip function B and go to function C if function B executing too long ?

我尝试了此方法,但似乎不起作用:

I tried this but seem not working :

try {
 const setTimer = setTimeOut({
  throw new Error("Time out!");
 },500);
 B();
 clearInterval(setTimer);
}
catch(error){
 console.warn(error);
}

但似乎不起作用.

更新1:仅供参考,我没有做任何反模式,但NPM中的功能B是其中的一部分,我向仓库的所有者提交了问题.试着躲开子弹,以便在修复之前我有一些额外的时间.谢谢你们到目前为止对我的帮助:)

Update 1 : FYI, I didn't do any anti-pattern but function B is something in NPM and I submitted issues to the owner of repo. Just try to dodge the bullet so I have some extra times until the fix. Thanks guys for helping me so far :)

推荐答案

setTimeout不能用于暂停执行函数

setTimeout 不能用于取消函数的执行. setTimeout 只是调用函数之前的延迟.简而言之,是的,您可以在执行之前将其停止,但是一旦执行,您将无法停止.

setTimeout cannot be used to halt the execution of a function

setTimeout cannot be used to cancel the execution of a function. setTimeout is simply a delay before your function is called. Which simply means, yes you can stop it before it's executed but once it's executed you can't.

一种解决方案是通过在函数中的各处放置检查来检查函数花费了多少时间.

A solution would be to check how much time the function is taking by placing checks a bit everywhere in your function.

function A(mili){
  const end = Date.now() + mili;
  for(let i = 0; i < 10000; i++){
    for(let j = 0; j < 10000; j++){
      if(Date.now() > end){
        console.log(mili + ": Halting A...");
        return;
      }
    }
  }

}

A(100); //halted
A(1000); //halted
A(10000); //passed

但这不能解决您的问题,因此不建议这样做,因为它要求您在关键点上添加许多检查",这些关键点可能会使函数在执行过程中花费的时间过长.

But this doesn't solve your problem and is not recommended because it requires you to add many "checks" at key points where your function may be taking time too long during execution.

这是假设您处于同步环境中

函数生成器不是一个完美的解决方案,但它是一个更好的解决方案.

Function generators is not a perfect solution but it is a better solution.

它需要对原始功能进行两个易于理解的修改.

It requires two easy to understand modifications of your original function.

  1. 在函数名称中添加 *
  2. 在函数的关键位置添加 yield 关键字,这可能被认为是执行时间长"
  1. Add * to your function name
  2. Add yield keyword in key spots in your function that may be deemed "long execution time"

最后,将您要测试的函数传递到某些包装器中,例如下面提供的 run 方法.

Finally, pass the function you wish to test in some wrapper like the run method provided below.

function* A(){
  for(let i = 0; i < 10000; i++){
    for(let j = 0; j < 1000; j++){
       yield;
    }
  }
  return "some final value";
}

function run(gen, mili){
  const iter = gen();
  const end = Date.now() + mili;
  do {
    const {value,done} = iter.next();
    if(done) return value;
    if(end < Date.now()){
      console.log("Halted function, took longer than " + mili + " miliseconds");
      return null;
    }
  }while(true);
}

console.log("Running...");
const res1 = run(A, 1000);
const res2 = run(A, 5000);
const res3 = run(A, 100);

console.log("run(A,1000) = " + res1); //halted
console.log("run(A,5000) = " + res2); //passed
console.log("run(A,100) = " + res3); //halted

这篇关于如果执行时间太长,则跳过该功能.的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:15