我正在尝试将视频输入仅在周四晚上7点到晚上8点之间显示,因为这是播放实时源的时间。 由于我不会进入的原因,我不允许在任何其他时间提起它。 由于我是JavaScript的新手,我确信我做错了什么,但从我能够学到的东西来看,这就是我所拥有的。
var hour = getHours() var day = getDay() document.onload=function timechange() { if day==4 && hour>19 && hour<21 document.getElementById(livefeed).innerHTML = "Text one" else document.getElementById(livefeed).innerHTML = "Text two" }这显然是示例文本,但大部分内容将在页面上更改。 我是否必须在HTML页面中添加某种功能才能使其正常工作?
I'm trying to get a video feed to only display on Thursdays between 7PM and 8PM because that's when the live feed is playing. For reasons I'll not go into, I'm not allowed to have it up at any other time. Since I'm new to JavaScript I'm sure I'm doing something wrong, but from what I've been able to learn this is what I've got.
var hour = getHours() var day = getDay() document.onload=function timechange() { if day==4 && hour>19 && hour<21 document.getElementById(livefeed).innerHTML = "Text one" else document.getElementById(livefeed).innerHTML = "Text two" }This is example text obviously but the majority of the content will change on the page. Do I have to add some sort of function in my HTML page to make this work?
最满意答案
对于简单版本,几乎就是这样。 getDay()和getHours()在Date类上定义:
function shouldShowVideo() { var currentDate = new Date(), currentDay = currentDate.getDay(), currentHour = currentDate.getHours(); return (currentDay == 4 && currentHour > 19 && currentHour < 20); } if (shouldShowVideo()) { // do something } else { // do something else }但是这种方法存在一个大问题。 因为你在JS中这样做,视频将被加载到你的页面上,无论如何(因此人们可能会获得它的链接)。 此外,因为在onload事件触发后您正在执行JS,所以可能会有几秒钟显示视频。 你可以做些什么来改进这个是在DOM Ready事件上运行这个代码(这里是jQuery助手: http : //api.jquery.com/ready/ )。
您最好的选择仍然是在服务器上移动此逻辑。 这样,你不必担心所有这些:没有涉及JS。
For the simple version, that's almost that. getDay() and getHours() are defined on the Date class:
function shouldShowVideo() { var currentDate = new Date(), currentDay = currentDate.getDay(), currentHour = currentDate.getHours(); return (currentDay == 4 && currentHour > 19 && currentHour < 20); } if (shouldShowVideo()) { // do something } else { // do something else }There is one big problem with this approach though. Because you do it in JS, the video will be loaded on your page, no matter what (so people could potentially get the link to it). Also, because you are executing your JS after the onload event fired, there could be a few seconds where the video will be displayed. What you can do to improve this is run this code on DOM Ready event instead (here is the jQuery helper: http://api.jquery.com/ready/).
Your best option would still be to move this logic on the server. That way, you don't have to worry about all this: no JS involved.
更多推荐
发布评论