js让网页在每天的指定的时间进行刷新

2016-08-17 18:46:01
这两天做了一个比较屌的前端页面, 算一个倒计时, 每天更换网页的皮肤。 我的思路是在每天更换不同的class, 只更换一个class,实现整个皮肤的更换。
 
var myDate = new Date(),
        el = 0,
        settime = new Date(2016, 7, 25), //设定时间
        time = settime - myDate; //计算毫秒数
    if (time < 0) { //如果超过了设定的时间
        el = 0; //使用最原始的皮肤
    } else {
        el = Math.ceil(time / 1000 / 3600 / 24); // 倒计时天数,换成当天的时间皮肤
    }
    var dayPic = ['day0.jpg', 'day1.jpg', 'day2.jpg', 'day3.jpg', 'day4.jpg', 'day5.jpg','day6.jpg'],
        path = '../img/day/';
    if (el > 6) {
        $('#timePic').attr('src', path + dayPic[6]);
        $('#day').addClass('day0');
    } else if (0 < el < 6) {
        $('#timePic').attr('src', path + dayPic[el]);
        $('#day').addClass('day' + el);
    } else {
        $('#timePic').attr('src', path + dayPic[0]);
        $('#day').addClass('day0');
    }
计算距离凌晨的时间, 并且让在改时间进行刷新
 
   var time = function(){
        //设定今晚上凌晨时候的时间
        var tommo = new Date();
        tommo.setHours(24);
        tommo.setMinutes(0);
        tommo.setSeconds(0);
        tommo.setMilliseconds(0);
        //获取现在的时间
        var now = new Date();
        var minus = parseInt((tommo - now)/1000); //现在距离凌晨的毫秒数
        if(minus==86399){ //第二天第一秒的时候进行刷新页面
            location.reload()
        }
    }
    setInterval(time, 1000);

关于

联系方式 :

mail: hey_cool@163.com ,
QQ:583459700

备案许可证编号:蜀ICP备16005545号-1 © COPYRIGHT 2015-2024 zhmzjl.com | by: KAPO