我想制作一个广告视频播放器,在其中可以在24小时内循环播放一组广告 让我们以这个json为例
var elements = '{"ads":[' +
'{"id":"1","name":"a","duration":"5","times":"6" },' +
'{"id":"2","name":"b","duration":"7","times":"2" },' +
'{"id":"3","name":"c","duration":"9","times":"4" },'+
'{"id":"4","name":"d","duration":"12","times":"1" },'+
'{"id":"5","name":"e","duration":"5","times":"5" }]}';
所以我需要至少显示6次广告“ a”,依此类推,在其余时间中,我需要在这些广告之间添加默认广告“ h”
这是我给的一个小开始,但它没有从JSON接收数据
function roundRobinRepeat(elems, repeats) {
var l = repeats.reduce((x, y) => x + y);
var w = Array(l), i = j = r = 0;
while (i < l) {
if (repeats[j] > r) w[i++] = elems[j];
j = (j + 1) % elems.length;
if (j === 0) r++;
}
return w;
}
document.getElementById("output").innerHTML = "Output: " + roundRobinRepeat(
["a", "b", "c", "d", "e"],
[ 6, 2, 4, 1 , 5]
).join(" ");