如何清除放置在.map回调函数内部的间隔

问题描述 投票:0回答:1

我在数组上使用map方法,以便设置将请求发送到API的间隔达到给定的次数(每个timeInterval具有不同的访问令牌)。我可以以某种方式创建一个可以从外部清除那些间隔的函数吗?

await Promise.all(
    this.state.tokens
       .map((token, index) => {
           const driver = setInterval(() => {
               if (decodedPolylines[index].length > 1) {
                   api.sendLocation(
                       token,
                       decodedPolylines[index][0][0].toString(),
                       decodedPolylines[index][0][1].toString()
                   );
               } else {
                   api.clockInOut(
                       token,
                       'out',
                       decodedPolylines[index][0][0].toString(),
                       decodedPolylines[index][0][1].toString()
                   );
                   clearInterval(driver);
               }
           }, 1000);
       })
);
javascript reactjs
1个回答
1
投票

该函数将清除所有间隔,但是如果您只希望清除一些间隔,也可以使用filter():

© www.soinside.com 2019 - 2024. All rights reserved.