如何在特定时间更改视频 [英] How to make a video change at specific times javascript
问题描述
我有问题.我希望网站中的视频链接每天随机更改其值.
Hi guys i have a problem. I want a video link in a website to change its value each day randomly.
示例:在星期一,视频链接1播放.在星期二,链接2播放,依此类推.我想用Javascript做到这一点.我尝试过这样的事情:
Example: On Monday video link 1 plays. On Tuesday link 2 plays and so on. I want to do this with Javascript. I tried something like this:
<iframe id="videorotator" style="box-shadow: 5px 5px 5px #888888;" width="307" height="315" src="//www.youtube.com/embed/UZopKnDHti8" frameborder="0" allowfullscreen></iframe>
<script type="text/javascipt">
var videos = ["http//www.youtube.com/embed/UZopKnDHti8","http://www.youtube.com/embed/z8GHNhteHJQ"];
var i=0;
function myLoop () {
setTimeout(function () {
var yourElement = document.getElementById('videorotator');
yourElement.setAttribute('src', "="+videos[i]);
i++;
if (i < 6) {
myLoop();
}
}, 3000)
}
myLoop();
</script>
JSFiddle此处: http://jsfiddle.net/4aearfvr/
JSFiddle here: http://jsfiddle.net/4aearfvr/
注意1:为了测试,我将此设置为3秒注意2:如果有更简便的解决方法,请通知我.谢谢
NOTE1: i set this to 3seconds for the sake of testing NOTE2: if there is an easier work around please inform me. thanks
推荐答案
如果您要根据一周中的某天显示视频,建议您这样做
If you want to display a video based on what day of the week it is, then I suggest you do it like this
var videos =["UZopKnDHti8","z8GHNhteHJQ","UZopKnDHti8","z8GHNhteHJQ","UZopKnDHti8","z8GHNhteHJQ","z8GHNhteHJQ"];
var day = new Date().getDay();
var yourElement = document.getElementById('videorotator');
yourElement.setAttribute( 'src', '//www.youtube.com/embed/' + videos[ day ] );
getDay
返回0 =星期日,1 =星期一,2 =星期二...非常适合访问数组
getDay
returns 0 = sunday, 1 = monday, 2 = tuesday ... which is perfect for accessing an array
在我仅添加ID的数组中,无需重复7次 http://www.youtube.com/embed/
,也可以在出现以下情况时将ID附加到网址中您使用 setAttribute
In the array I added just the id's, there's no need to repeat http://www.youtube.com/embed/
7 times, might as well just append the ID to the url when you use setAttribute
在演示中,您可以看到从原始视频到正确视频的缩略图轻拂,只需删除原始src,它将更流畅地工作
In the demo you can see the thumbnail flick from the original video to the correct one, simply remove the original src and it'll work smoother
这篇关于如何在特定时间更改视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!