SetInterval 函数未在小时内设置 innerHTML
Posted
技术标签:
【中文标题】SetInterval 函数未在小时内设置 innerHTML【英文标题】:SetInterval function not setting innerHTML on top of the hour 【发布时间】:2015-11-09 04:27:50 【问题描述】:我正在尝试制作一个具有我选择的完整时间表的人工和动态网络广播电台。但是,当我在 SetInterval 函数中运行 Check() 函数时,它似乎并没有替换现有的 P 标记。我开始怀疑我的问题是否与 if 参数有关...
<script type="text/javascript">
marklevin = "<p><button onclick='Control()'>Pause</button><br /><div><embed type='application/x-vlc-plugin' pluginspage='http://www.videolan.org' width='50%' height='20%' id='vlc' loop='yes' autoplay='yes' target='http://4293.live.streamtheworld.com/MARK_LEVINAAC?&uuid=15F6FB00-0000-0001-1755-1EBAEDA617CA'></embed></div></p>";
laura = "<p><button onclick='Control()'>Pause</button><br /><div><embed type='application/x-vlc-plugin' pluginspage='http://www.videolan.org' width='50%' height='20%' id='vlc' loop='yes' autoplay='yes' target='http://noxsolutions.mp3.miisolutions.net/nox/laura'></embed></div></p>";
sean = "<p><button onclick='Control()'>Pause</button><br /><div><embed type='application/x-vlc-plugin' pluginspage='http://www.videolan.org' width='50%' height='20%' id='vlc' loop='yes' autoplay='yes' target='http://prn-hannity-ic-pri.ng.akacast.akamaistream.net/7/811/60538/v1/auth.akacast.akamaistream.net/prn_hannity_ic_pri'></embed></div></p>";
cbs = "<p><button onclick='Control()'>Pause</button><br /><div><embed type='application/x-vlc-plugin' pluginspage='http://www.videolan.org' width='50%' height='20%' id='vlc' loop='yes' autoplay='yes' target='http://1451.live.streamtheworld.com:3690/CBSNEWSDIALUP_SC'></embed></div></p>";
control = 0
divs = document.getElementsByTagName("div");
ps = document.getElementsByTagName("p");
var currentdate = new Date();
hours = currentdate.getHours();
minutes = currentdate.getMinutes();
seconds = currentdate.getSeconds();
if (hours > 0 && hours < 4)
document.write("Laura Ingraham Show<br />Midnight to 4");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerhtml;
if (hours == 4)
document.write("Mark Levin Show<br />4 AM");
document.write(marklevin);
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 5)
document.write("CBS Radio News<br />5 AM");
document.write(cbs);
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours == 6)
document.write("Sean Hannity show<br />6 AM");
document.write(sean);
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 7)
document.write("Laura Ingraham Show<br />7 AM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 8)
document.write("CBS Radio News<br />8 AM");
document.write(cbs);
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours > 8 && hours < 11)
document.write("Sean Hannity Show<br />9 AM to 11 AM");
document.write(sean);
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 11)
document.write("Laura Ingraham Show<br />11 AM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 12)
document.write("Mark Levin Show<br />NOON");
document.write(marklevin);
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours > 12 && hours < 15)
document.write("CBS Radio News<br />1 PM to 3 PM");
document.write(cbs);
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours == 15)
document.write("Mark Levin Show<br />3 PM");
document.write(marklevin);
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 16)
document.write("Laura Ingraham Show<br />4 PM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 17)
document.write("Sean Hannity Show<br />5 PM");
document.write(sean);
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 18)
document.write("Laura Ingraham Show<br />6 PM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours > 18 && hours < 21)
document.write("Mark Levin Show<br />7 PM to 9 PM");
document.write(marklevin);
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 21)
document.write("Laura Ingraham<br />9 PM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 22)
document.write("Sean Hannity Show<br />10 PM");
document.write(sean);
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 23)
document.write("Mark Levin Show<br />11 PM");
document.write(marklevin);
set2 = "marklevin";
div_content = divs[0].innerHTML;
function Check()
currentdate = new Date();
hours = currentdate.getHours();
minutes = currentdate.getMinutes();
seconds = currentdate.getSeconds();
if (hours > 0 && hours < 4 && set2 != "laura")
ps[0].innerHTML = "Laura Ingraham Show<br />Midnight to 4<br />" + laura
set2 = "laura";
if (hours == 4 && set2 != "marklevin")
ps[0].innerHTML = "Mark Levin Show<br />4 AM<br />" + marklevin
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 5 && set2 != "cbs")
document.write("CBS Radio News<br />5 AM");
document.write(cbs);
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours == 6 && set2 != "sean")
document.write("Sean Hannity show<br />6 AM");
document.write(sean);
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 7 && set2 != "laura")
document.write("Laura Ingraham Show<br />7 AM");
document.write(laura);
set2 = "laura";
div_content = divs[0].innerHTML;
// Copy this code throughout the entire page. It's more stable than before.
// ps[0].innerHTML =
// Instead of
// document.write(
if (hours == 8 && set2 != "cbs")
ps[0].innerHTML = "CBS Radio News<br />8 AM<br />" + cbs;
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours > 8 && hours < 11 && set2 != "sean")
ps[0].innerHTML = "Sean Hannity Show<br />9 AM to 11 AM<br />" + sean;
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 11 && set2 != "laura")
ps[0].innerHTML = "Laura Ingraham Show<br />11 AM<br />" + laura;
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 12 && set2 != "marklevin")
ps[0].innerHTML = "Mark Levin Show<br />NOON<br />" + marklevin;
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours > 12 && hours < 15 && set2 != "cbs")
ps[0].innerHTML = "CBS Radio News<br />1 PM to 3 PM" + cbs;
set2 = "cbs";
div_content = divs[0].innerHTML;
if (hours == 15 && set2 != "marklevin")
ps[0].innerHTML = "Mark Levin Show<br />3 PM" + marklevin;
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 16 && set2 != "laura")
ps[0].innerHTML = "Laura Ingraham Show<br />4 PM" + laura;
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 17 && set2 != "sean")
ps[0].innerHTML = "Sean Hannity Show<br />5 PM" + sean;
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 18 && set2 != "laura")
ps[0].innerHTML = "Laura Ingraham Show<br />6 PM" + laura;
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours > 18 && hours < 21 && set2 != "marklevin")
ps[0].innerHTML = "Mark Levin Show<br />7 PM to 9 PM" + marklevin;
set2 = "marklevin";
div_content = divs[0].innerHTML;
if (hours == 21 && set2 != "laura")
ps[0].innerHTML = "Laura Ingraham<br />9 PM" + laura;
set2 = "laura";
div_content = divs[0].innerHTML;
if (hours == 22 && set2 != "sean")
ps[0].innerHTML = "Sean Hannity Show<br />10 PM" + sean;
set2 = "sean";
div_content = divs[0].innerHTML;
if (hours == 23 && set2 != "marklevin")
ps[0].innerHTML = "Mark Levin Show<br />11 PM" + marklevin;
set2 = "marklevin";
div_content = divs[0].innerHTML;
function Control()
just_set = 0
if (control == 0 && just_set != 1)
divs[0].innerHTML="<p>Paused</p>";
control = 1;
just_set = 1
if (control == 1 && just_set != 1)
divs[0].innerHTML=div_content;
just_set = 1;
control = 0;
Check()
myVar = setInterval(Check, 1000);
window.i = 0
//alert(window.i)
function buttonFunction()
//alert(window.i);
//alert(window.i);
if (window.i > 1)
//alert(window.i)
window.i = 0;
if (window.i == 0)
var currentdate = new Date();
hours = currentdate.getHours();
minutes = currentdate.getMinutes();
seconds = currentdate.getSeconds();
days = currentdate.getDay();
months = currentdate.getMonth();
years = currentdate.getFullYear();
years_to_seconds = years * 11404800;
months_to_seconds = months * 950400;
days_to_seconds = days * 86400;
hours_to_seconds = hours * 3600;
minutes_to_seconds = minutes * 60;
window.start_time = years_to_seconds + months_to_seconds + days_to_seconds + hours_to_seconds + minutes_to_seconds + seconds;
if (window.i == 1)
var currentdate = new Date();
hours2 = currentdate.getHours();
minutes2 = currentdate.getMinutes();
seconds2 = currentdate.getSeconds();
days2 = currentdate.getDay();
months2 = currentdate.getMonth();
years2 = currentdate.getFullYear();
years_to_seconds2 = years2 * 11404800;
months_to_seconds2 = months2 * 950400;
days_to_seconds2 = days2 * 86400;
hours_to_seconds2 = hours2 * 3600;
minutes_to_seconds2 = minutes2 * 60;
end_time = years_to_seconds2 + months_to_seconds + days_to_seconds2 + hours_to_seconds2 + minutes_to_seconds2 + seconds2;
total_tim = end_time - window.start_time;
total_time = total_tim.toString();
alert("It's been " + total_time + " seconds since you hit the button.");
document.cookie="timeittook=" + total_time;
window.i = window.i + 1;
</script>
此外,当我将 setInterval 命令中对 Check() 的引用从 Check() 更改为 Check 时,页面挂起,即使这是正确的语法。
【问题讨论】:
您的代码太长了。阅读How to Ask 你什么时候测试过代码?您将document.write
用于某些输出,这将覆盖当前页面。此外,在 0:00 和 1:00 之间有一个间隙,它不会做任何事情。
我从晚上 11 点运行到早上 8 点左右。当我说它“挂起”时,它就像完全冻结了,小沙滩球在旋转。
另外,我相信我所有的 document.write 声明都在开头。没有它们,p 标签一开始就不会存在,除非我在 HTML 中这样做。
【参考方案1】:
啊哈!我已经修好了。我有多个问题:
-
VLC 插件被冻结。这就是它挂起的原因。
我的代码确实更新了,但我无法判断,因为笔记本电脑已关闭并处于睡眠状态。
【讨论】:
以上是关于SetInterval 函数未在小时内设置 innerHTML的主要内容,如果未能解决你的问题,请参考以下文章
jQuery 使用 $(this) 作为 setInterval/setTimeOut 函数内的起点遍历 DOM?