简体   繁体   English

clearInterval() 在 typescript 中使用时抛出“没有重载匹配此调用”

[英]clearInterval() throwing 'No overload matches this call' when used in typescript

I am trying to write a setInterval function (which works as expected), but it should be cleared once this.attempts hits the level of this.retryTimes.我正在尝试编写一个 setInterval function(按预期工作),但是一旦 this.attempts 达到 this.retryTimes 的级别,它就应该被清除。 When I try to call clearInterval() as shown below, I am given the following error:当我尝试如下所示调用 clearInterval() 时,出现以下错误:

No overload matches this call.
  Overload 1 of 2, '(intervalId: Timer): void', gave the following error.
    Argument of type '() => void' is not assignable to parameter of type 'Timer'.
      Type '() => void' is missing the following properties from type 'Timer': ref, unref
  Overload 2 of 2, '(handle?: number): void', gave the following error.
    Argument of type '() => void' is not assignable to parameter of type 'number'.
const retryLoop = () => {
      if (this.shouldRetry) {
        this.attemptRetry();
        this.showError = true;
      }
      this.attempts = this.attempts + 1;
    };
    const stopRetry = () => {
      clearInterval(retryLoop);
    }
      setInterval(retryLoop, 5000);
      if (this.attempts > this.retryTimes) {
        clearInterval(stopRetry)
      }
    }

In order to clear an interval (with clearInterval ) you need to take the return from setInterval you almost have it:为了清除间隔(使用clearInterval ),您需要从setInterval获取回报,您几乎拥有它:

function something() {
    const retryLoop = () => {
        if (this.shouldRetry) {
            this.attemptRetry();
            this.showError = true;
        }
        this.attempts = this.attempts + 1;
    };
    const stopRetry = setInterval(retryLoop, 5000);
    if (this.attempts > this.retryTimes) {
        clearInterval(stopRetry);
    }
}

You can ignore the function something() { line.. there was just a mismatched close bracket at the end so I wrapped it (to run an auto formatter on it)您可以忽略function something() { line.. 最后只有一个不匹配的右括号,所以我将它包装起来(在其上运行自动格式化程序)

clearInterval(xxxxx): here xxxx should be the setInterval itself, not the function ( retryLoop ). clearInterval(xxxxx):这里 xxxx 应该是setInterval本身,而不是 function ( retryLoop )。


w3school link: https://www.w3schools.com/jsref/met_win_clearinterval.asp w3school 链接: https://www.w3schools.com/jsref/met_win_clearinterval.asp

The clearInterval() method clears a timer set with the setInterval() method. clearInterval() 方法清除使用 setInterval() 方法设置的计时器。

myVar = setInterval("javascript function", milliseconds);

clearInterval(myVar);


changed the timer from '5000' to '1000' just for quick execution.将计时器从“5000”更改为“1000”只是为了快速执行。

 let attempts = 1; const retryTimes = 10; const retryLoop = () => { console.log("retry no: "+attempts) attempts = attempts + 1; if (attempts > retryTimes) { console.log("---- ending retry ----") clearInterval(attemptRetry); } }; var attemptRetry = setInterval(retryLoop, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript papaparse 没有过载匹配这个调用 - Typescript papaparse No overload matches this call 无法编译 TypeScript - 没有与此调用匹配的重载 - Unable to compile TypeScript - No overload matches this call Typescript 路由错误 - 没有与此调用匹配的过载 - Typescript Routing Error - No overload matches this call NodeJS“没有重载匹配这个调用。” 打字稿错误 - NodeJS "No overload matches this call." error with typescript Materia-ui 和 typescript 没有重载匹配此调用 - Materia-ui and typescript No overload matches this call expressJS 的 TypeScript 错误:没有与此调用匹配的重载 - TypeScript Error for expressJS: No overload matches this call 没有重载匹配这个调用。 最后一次重载在打字稿中出现以下错误 - No overload matches this call. The last overload gave the following error in typescript 为什么我在使用数组 concat 时不断收到 TypeScript 错误“没有重载匹配此调用”? - Why am I keep getting TypeScript error “No overload matches this call” when using Array concat? 我是打字稿的新手当我编译我的代码时,没有重载匹配这个调用错误 - I am new in typescript When I compile my code given No overload matches this call error 调用 axios 时出现 Typescript 错误 - 没有与此调用匹配的过载 - Typescript error while calling axios - no overload matches this call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM