用javaScript写的这个计时器为什么会出错?

用javaScript写了一个计时器,然后设置1秒钟刷新一次。
相同的页面下同样也用jQuery.nicescroll写了一个滚动条。
但是呢,写好之后发现滚动条竟然也会随着计时器的记时而不断地display:none——display:block
这个怎么破?求助非常感谢!

项目地址:http://hkhgxx.azurewebsites.n...

代码:

//首先,是滚动条的设置
$(document).ready(function() { 
    'use strict';
    $("body").niceScroll({
        cursorcolor: "#F65E2F",
        cursorwidth: '4px',
        zindex: 999998,
        cursorborder: "0",
        cursorborderradius: "0",
        railpadding: { top: 4, right: 4, left: 0, bottom: 4 },
        horizrailenabled: false,
        boxzoom:true,
        autohidemode:false
    });
});
//这里是写了一个div方便操作滚动条
$(document).ready(function(){
    'use strict';
    $(".container").after("<div class='touchArea-cursor'></div>");
    $(".touchArea-cursor").css("height",window.innerHeight);
    $(".touchArea-cursor").hover(function(){
        $(".nicescroll-cursors").css("width","12px");
    },function(){
        $(".nicescroll-cursors").css("width","4px");
    });
});
//这里是倒计时函数。
function whatisGaokao(){
    'use strict';
    
    var date =new Date();
    date.setFullYear(2018,6,7,8);
    var today = new Date();
    
    var Ytoday = today.getFullYear();
    var Mtoday = today.getMonth() + 1;
    var Dtoday = today.getDate();
    var dateT = Mtoday + '-' + Dtoday + '-' + Ytoday;
    
    var TheDayOfGaokao = "7-6-2018";
    
    var stringDateToday  =  dateT.split("-");
    var odate1  =  new  Date(stringDateToday[0]  +  '-'  +  stringDateToday[1]  +  '-'  +  stringDateToday[2]); 
    var stringDateTodayOut  =  TheDayOfGaokao.split("-");
    var odate2  =  new  Date(stringDateTodayOut[0]  +  '-'  +  stringDateTodayOut[1]  +  '-'  +  stringDateTodayOut[2]);
    
    var iDaysY  =  parseInt(Math.abs(odate1  -  odate2)  /  1000  /  60  /  60  /24);
    var iDaysH  =  " ";
    var iDaysM  =  " ";
    var iDaysS  =  " ";
    
    var SDTH = today.getHours();
    if (SDTH > 12){
        SDTH = Math.ceil(SDTH / 2);
        SDTH = 12 - SDTH;
        iDaysH  =  Math.abs(8  +  SDTH);
    }else if(SDTH > 8){
        SDTH = 12 - SDTH;
        iDaysH  =  Math.abs(20  -  SDTH);
    }else{
        iDaysH  =  Math.abs(SDTH  -  8);
    }
    
    var SDTM = today.getMinutes();
    iDaysM = 59 - SDTM;
    
    var SDTS = today.getSeconds();
    iDaysS = 59 - SDTS;
    
    var SDOH = today.getHours();
    var SDOM = today.getMinutes();
    var SDOS = today.getSeconds();
    
    var back = " ";
    var feedback = " ";
    var so = " ";
    
    if (date > today){
        back = "距离高考还有";
        if (iDaysY < 10){
            feedback = "还看什么看啊!离高考只有十天不到了!还不快点去复习!";
            so = back + iDaysY + "天 " + iDaysH + "小时" + iDaysM + "分钟" + iDaysS + "秒";
        }else if (iDaysY > 10){
            feedback = "不过,也不用太着急,浏览这个网站的时间还是有的嘛!";
            so = back + iDaysY + "天 " + iDaysH + "小时" + iDaysM + "分钟" + iDaysS + "秒";
        }
    }
    else{
        back = "高考已经过去了";
        feedback = "啊, 回想起来还是满满的回忆啊!";
        so = back + iDaysY + "天 " + SDOH + "小时" + SDOM + "分钟" + SDOS + "秒";
    }
    
    var soWhat = "<p style=\"text-align: center;line-height: 72%;font-size: 48px\">" + so + "</p><p class=\"lead\" style=\"text-align: center\">" + feedback + "</p>";
    
    $(".gaokao").html(soWhat);
}

$(document).ready(function(){
    'use strict';
    window.setInterval(function(){whatisGaokao();},1000);
});
//问题来了,这个计时器为什么也会对滚动条产生作用?
阅读 2.8k
2 个回答

clipboard.png
没细看代码,上图,nicescroll-cursor是包含在div ascrail12000里的,后者在定时器的回调中不断出现消失,故前者也一样

$(document).ready(function() { 
    whatisGaokao()//调用方法
    $("body").niceScroll({
        cursorcolor: "#F65E2F",
        cursorwidth: '4px',
        zindex: 999998,
        cursorborder: "0",
        cursorborderradius: "0",
        railpadding: { top: 4, right: 4, left: 0, bottom: 4 },
        horizrailenabled: false,
        boxzoom:true,
        autohidemode:false
    });
});
function whatisGaokao(year,month,day,hour,minute,second){
    var leftTime = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    $(".gaokao").html(days+"天" + hours+"小时" + minutes+"分"+seconds+"秒");
    setInterval("whatisGaokao(2016,6,6,6,6,6)",1000);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题