我在数组上使用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);
})
);
该函数将清除所有间隔,但是如果您只希望清除一些间隔,也可以使用filter():