2 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要將間隔保存到一個(gè)變量中,然后您可以在該變量clearInterval()上使用。
這是您要完成的任務(wù)的模型。
var array = [];
var maxLength = 3;
var delay = 250; //I shortened your delay
var ticker = {}; //I'll use this to simulate your ticker object
var looper = setInterval(function() {
ticker.BNBBTC = Math.random(); //populating your ticker property w/random value
if (array.length < maxLength) {
array.push(ticker.BNBBTC);
} else {
console.log("Stopping the looper.");
clearInterval(looper);
console.log("Here are the contents of array");
console.log(array);
}
}, delay);

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
我不確定我是否理解你的目的,因?yàn)槟抢镉泻芏嘧⑨尨a,但如果你想運(yùn)行一個(gè)函數(shù)三次并在一秒鐘后以新價(jià)格再次運(yùn)行它,或者......可能這段代碼對你有幫助:
let array = [];
let eachEverySeconds = 1;
const loopArray = (array) => {
setTimeout(async () => {
if (array.length === 3) return;
let price = Math.random() * 10;
array.push(price);
await loopArray(array);
}, 1000 * eachEverySeconds);
console.log(array);
};
loopArray(array);
添加回答
舉報(bào)