這個code如果有很多視頻,該如何簡化?

var video22 = document.getElementById("video22");
var intervalID = null;
var time = 0;
var speed = 15;
video22.onloadedmetadata = function() {
    var duration = video22.duration;
    intervalID = setInterval(function(){
        time++;
        if (time / speed <=  duration){
            video22.currentTime = time / speed;
        }else{
            clearInterval(intervalID);
            intervalID = null;
        }
    },1000 / speed);
};
video22.addEventListener('click', function(){
    clearInterval(intervalID);
    intervalID = null;
    this.play();
});

想問一下這個
如果這個code如果有很多視頻
我該如何簡化呢
因為這樣有幾個視頻就要幾段這樣的code

阅读 1.6k
2 个回答
var list=docuemnt.querySeclectotAll("video");
for(var i=0,len=list.length;i<len;i++){
    list[i].onloadedmetadata=function(){
        .....
    }
    list[i].addEventListener('click', function(){
        ...
    })
}
这样?

写成一个直接执行函数?

(function(視頻ID){
var video22 = document.getElementById(視頻ID);
函数体...
})(視頻ID)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题