在午夜使用 Javascript/Jquery/Ajax 或 HTML5 加载特定页面



我承认这听起来有点正统。

但是,我想知道是否有一种方法可以使用Javascript/Jquery/Ajax或HTML5加载特定网站?

我问的原因是因为我知道有一种使用 cronjobs 的方法,但我很好奇是否有另一种方法不需要 cron 作业即可使其运行。

通过加载特定页面,我指的是任何真正的东西,也许是一组 的文字或 iframe 。

  • 德里克

你必须已经加载了一个页面,所以假设这是 index.html 中的 javascript:

var now = new Date(),
    then = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate(),
        0,0,0),
    diff = now.getTime() - then.getTime();

这将看到自午夜以来的毫秒数。 现在,如果午夜1分钟内,下面的代码将重定向到页面(或文本):

document.onload = function()
{
    var now = new Date(),
        then = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate(),
            0,0,0),
        diff = now.getTime() - then.getTime();
    if(diff <= 60000)
    {
        //Un-comment first line to go to page, second to change text of page
        //window.location.href = 'pageToLoad.html';
        //document.body.innerHTML = 'yourText';
    }
}

就像你已经知道有cron作业(服务器端)允许你在服务器/主机上的精确时间执行一些php脚本。

如果您希望您的用户通过使用 ajax 加载不同的内容在一天中的某个时间(或特定时间)看到不同的页面。这是一个非常简短的例子。

1.现代浏览器的ajax功能(Chrome,Safari,IE10,iOS,Android..)

2.时间检查

3.获取夜间或白天的内容。

function ajax(a,b,c){//url,function,just a placeholder
 c=new XMLHttpRequest;
 c.open('GET',a);
 c.onload=b;
 c.send()
}
var h=new Date().getHours();
// if it's after 12pm & before 6am it returns night else day.
nightday=(h>0&&h<6?'night':'day')+'.php';
//get the data from file 'day.php' or 'night.php'
ajax(nightday,function(){
 //this.response is the content
 console.log(this.response);
});

如果您只想执行一次:

 window.onload=function(){
  var c=new XMLHttpRequest,h=new Date().getHours();
  c.open('GET',(h>0&&h<6?'night':'day')+'.php');
  c.onload=function(){console.log(this.response)};
  c.send()
 }

从这里开始,现在有多种方法可以检查现在几点。

在每次点击,在某些特定的点击上,设置超时(坏),设置间隔(坏)。等等。

夜.php

<?php
//load the content for night
?>

天.php

<?php
//load the content for day
?>

最新更新