我正在尝试编写一个脚本,允许我在每个星期五的特定时间重定向到一个网页。
希望将脚本重定向到实时视频源的Iframe,并在一小时后,让脚本也重定向到一个html文件,该文件将存储在运行启动页面的PC上,直到下一周的下一个Feed,将根据日期和时间再次启动脚本。
在过去的3个小时里,我一直试图从堆栈溢出中发现的脚本中挽救一些东西但没有成功。非常感谢对此有所帮助!
我希望这对你有用。
function myFunction() {
var d = new Date();
var n = d.getDay()
var time=.getHours()
if(n==5)
{
//based on time
if(time==14)
{
window.location.href="www.YourRedirectpage.com";
}
}
这应该工作(ES5语法):
Date.prototype.hour = function () {return (this.getHours())}
Date.prototype.day = function () {return (this.getDay())}
var today = new Date()
if (today.hour() == "10" && today.day() == "6") {
// change you url here, such as; location.href ="friday url";
}
else {
// keep (or re-attribute) your base url, such as; location.href ="base url";
}
我想你想要在UI中进行某种简化的工作,它会继续观察并为你重定向,而你不需要手动干预。您应该使用Javascript中的setTimeout来实现此目的。
这个解决方案的作用是计算到星期五与特定时间到当前日期时间之间的毫秒差异,并开始超时事件。
希望这很容易理解并帮助你。
GIT Repo:https://github.com/helloritesh000/how-to-redirect-browser-at-specific-date-and-time
<!DOCTYPE html>
<html>
<body onload="RedirectTo(5, 15, 49, 30);"> <!-- RedirectTo(day(1-7(Monday)-(Sunday)),1-24 hour,1-60 min,1-60 sec) -->
<h1>This will reload redirect page</h1>
@ - <p id="demo"></p>
<script>
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
function RedirectTo(day, hour, min, sec) {
var d = new Date(getNextDayOfWeek(new Date(), day));
d.setHours(hour);
d.setMinutes(min);
d.setSeconds(sec);
document.getElementById("demo").innerHTML = d;
var totalMilliSecDiff = d-new Date();
if(totalMilliSecDiff > 0)
{
setTimeout(function(){ window.location.href = "http://www.google.com"; }, totalMilliSecDiff);
}
}
</script>
</body>
</html>