我正在获取JSON文件并将其绑定到列表项。我已经为每行的json添加了名称/值对作为audioTime /“时间类似于0,5,10秒的形式”。
我在cordova应用程序中使用媒体插件,并执行如下功能:
setInterval(() => {
if (this.loadedFullData == false) {
this.data = this.dataMain; // data is large so I have sliced data into slices in this.data variable. Now bind full data to sliced data variable. I think it takes 2-5 secs based on devices and freeze app for 1-2 secs.
this.loadedFullData = true;
}
this.mediaObject.getCurrentPosition().then((currentTime) => {
this.mediaCurrentTime = currentTime;
let divToUpdate = 'Eng' + Math.floor(currentTime);
if ($('#' + divToUpdate).length) {
$('#' + this.oldDivId).css({
'font-weight': 'normal'
});
$('#' + divToUpdate).css({
'font-weight': 'bold',
});
window['customComponent'].component.content.scrollTo(0, document.getElementById(divToUpdate).offsetTop - (screen.height / 3), 1000);
this.oldDivId = divToUpdate;
}
});
}, 1000);
现在问题是:它走向正确但有时会出错。有什么问题?我很困惑。有没有其他方法???
我认为问题在于这一行
if ($('#' + divToUpdate).length)
如果你知道你的元素在表单“Eng”+ someNumber上有这样的id:
let divToUpdate = 'Eng' + Math.floor(currentTime);
然后你不必检查长度。您可能最终得到一个nullpointer。
怎么样
this.mediaCurrentTime = currentTime;
var divToUpdate = 'Eng' + Math.floor(currentTime);
var divToUpdateEl = document.getElementById(divToUpdate);
if (divToUpdateEl) {
$('#' + this.oldDivId).css({
'font-weight': 'normal'
});
$('#' + divToUpdate).css({
'font-weight': 'bold',
});
divToUpdateEl.scrollIntoView();
this.oldDivId = divToUpdate;
}