3 回答

TA貢獻1848條經(jīng)驗 獲得超2個贊
如果你使用 dict 和 setInterval 會更容易
const words = {
0: "hello",
200: "world",
800: "I",
900: "am",
1300: "John",
2000: "Smith"
}
function speak(timing) {
if(timing in words) {
console.log(words[timing])
}
}
current_timestamp = 0
function timer() {
speak(current_timestamp)
current_timestamp += 100
}
setInterval(timer, 100);

TA貢獻1877條經(jīng)驗 獲得超1個贊
const timing = [0, 0.2, 0.8, 0.9, 1.3, 2];
const words = ['hello', 'world', 'I', 'am', 'John', 'Smith'];
function getWord(timing, words, time) {
return words[timing.indexOf(time)];
}
console.log(getWord(timing, words, 0.9));

TA貢獻1777條經(jīng)驗 獲得超10個贊
我認為你可以這樣做:
const timing = [0, 0.2, 0.8, 0.9, 1.3, 2]
const words = ["hello", "world", "I", "am", "John", "Smith"];
var hashTable = {}
timing.forEach((i, index)=>{
hashTable[i] = words[index];
});
console.log(hashTable)
console.log(hashTable[timing[3]])
添加回答
舉報