在我的应用中,用户可以开始录制,他们触摸的每个按钮都会保存到数组中
例如:
"[{"time":1835,"elemId":"bass"},{"time":2553,"elemId":"highhat"}]"
然后我有一个函数引入数组并且应该播放每次触摸但是,我现在的代码只是出现错误
document.getElementById('playback').onclick = function () {
console.log(localStorage.getItem("eventlist"));
var playback = JSON.parse(window.localStorage.getItem("eventlist"))
for (i = 0; i < playback.length; i++) {
play = playback[i];
setTimeout((function (play) {
return function () {
document.getElementById(play.elemId).touchstart();
}
})(play), play.time)
}
}
error: Uncaught TypeError: Object #<HTMLDivElement> has no method 'touchstart'.
我很感激任何有关此事的帮助,因为我已经被卡住了,提前谢谢!
HTML:<div class="drum" id="bass" ontouchstart="play('bass');" ontouchend="touchEnd(event);">Bass</div>
答案 0 :(得分:1)
我建议将引用嵌入到功能符号或字符串参数中,以描述所播放的乐器而不是元素&#39;标识。它是更快,更优雅的解决方案。
"[{"time":1835,"elemId":"bass"},{"time":2553,"elemId":"highhat"}]"
并且
for (i = 0; i < playback.length; i++) {
instrument = playback[i];
setTimeout(function (instrument) {
play(instrument.elemId)
}, instrument.time)
}