简体   繁体   English

如何在特定时间重定向页面

[英]How to redirect a page at a specific time

我正在尝试找到一种方法,以便每天在美国东部标准时间晚上7:58将页面重定向到另一页面,然后每天在美国东部标准时间晚上9点重定向回该页面,对此有什么想法或建议吗?

I created something for you to trigger your function every x seconds and redirect to wherever you want 我为您创建了一些东西,每隔x秒触发一次函数,然后将其重定向到您想要的任何位置

window.setInterval(function(){ //This will trigger every X miliseconds

    var date = new Date(); //Creates a date that is set to the actual date and time

    var hours = date.getHours(); //Get that date hours (from 0 to 23)
    var minutes = date.getMinutes(); //Get that date minutes (from 0 to 59) 

   if (hours == 7 && minutes == 58 ){
       //redirect to where you want
   } else if( hour = 21 && minutes == 00){
       //redirect to where you want
   } 

}, 1000); //In that case 1000 miliseconds equals to 1 second

Now, talking about UTC and setting it. 现在,讨论UTC并进行设置。 You can set it when you create your date, there is a topic talking only about that: Create a Date with a set timezone without using a string representation AND How to initialize a JavaScript Date to a particular time zone 您可以在创建日期时进行设置,这里只涉及一个主题: 使用设置的时区创建日期而不使用字符串表示形式 ,以及如何将JavaScript日期初始化为特定时区

Sometimes I would recomend using something like moment.js. 有时我会推荐使用诸如moment.js之类的东西。 I hope my answer helps you someway 希望我的回答对您有所帮助

Redirecting to another page is quite straight forward, however redirecting back to the originating page isn't as easy. 重定向到另一个页面很简单,但是重定向回到原始页面并不容易。

To achieve this effect, you might consider wrapping the pages you want to navigate to and from, in an <iframe> element, where the page containing the <iframe> controls the navigation at those key times. 为了实现此效果,您可以考虑将要导航的页面包装在<iframe>元素中,其中包含<iframe>的页面在那些关键时刻控制导航。

You could also consider setting up up an interval to check for and run this navigation logic (if needed) every one minute using setInterval() . 您还可以考虑使用setInterval()每隔一分钟设置一个间隔来检查和运行此导航逻辑(如果需要setInterval() To simplify the manipulation and querying of times (in relation to your timezone), you might find momentjs helpful. 为了简化时间的操作和查询(相对于您的时区),您可能会发现momentjs很有帮助。

These ideas are combined in the snippet below: 这些想法在以下代码段中进行了组合:

 /* Run this logic once per minute */ const oncePerMinute = () => { /* Get iframe and current iframe src */ const iframe = document.querySelector('iframe'); const iframeSrc = iframe.getAttribute('src'); /* The url's to display for either time slot */ const dayUrl = 'daytimeurl' const nightUrl = 'nighttimeurl' /* Adjust time to EST (-5hr) */ const momentEST = moment().utc().utcOffset('-0500'); /* If between 7:58am and 9:00pm update iframe to show day url */ if(momentEST.isBetween(moment('7:58am', 'h:mma'), moment('9:00pm', 'h:mma'))) { if(iframeSrc !== dayUrl) { iframe.setAttribute('src', dayUrl); } } /* Othwise, update iframe to show night url */ else { if(iframeSrc !== nightUrl) { iframe.setAttribute('src', nightUrl); } } } /* Will set interval to run oncePerMinute every minute */ setInterval(oncePerMinute, 1000 * 60); oncePerMinute(); 
 iframe { position:fixed; top:0; left:0; width:100%; height:100%; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script> <iframe></iframe> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM