我正在建立一个转录服务的音频或视频文件与反应。
后端已经完成了,我正在获取这两个数组:
我有一个时间数组,以秒为单位:
const timing = [0, 0.2, 0.8, 0.9, 1.3, 2]我还有一系列的单词:
const words = ["hello", "world", "I", "am", "John", "Smith"]我只想在时机合适的时候突出显示一个单词。
除了"if“语句的无穷无尽之外,还有其他方法吗?
if(time = timing[0]) {return words[0]}
if(time = timing[1]) {return words[1]}
...
if(time = timing[n]) {return words[n]}(*时间等于视频/音频播放器)
谢谢!
发布于 2020-06-22 17:11:33
如果你使用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);
发布于 2020-06-22 17:02:01
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));
发布于 2020-06-22 17:52:19
我认为你可以这样做:
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]])
https://stackoverflow.com/questions/62510769
复制相似问题