3 回答
TA贡献1848条经验 获得超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条经验 获得超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条经验 获得超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]])
添加回答
举报
