Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setInterval导致了错误的函数_Javascript - Fatal编程技术网

Javascript setInterval导致了错误的函数

Javascript setInterval导致了错误的函数,javascript,Javascript,当我在setInterval内运行倒计时函数时,它每秒返回相同的输出,但传递给函数的值会发生变化。这是 以下是工作示例代码: var date=新日期2017,3,27,21.getTime; 函数倒计时毫秒{ 变量CDDate={ 秒:0, 分钟:0, 小时:0, 天数:0 } x=毫秒; CDDate.seconds=Math.roundx+30%60; x=x/60; CDDate.minutes=Math.roundx%60; x=x/60; CDDate.hours=Math.rou

当我在setInterval内运行倒计时函数时,它每秒返回相同的输出,但传递给函数的值会发生变化。这是


以下是工作示例代码:

var date=新日期2017,3,27,21.getTime; 函数倒计时毫秒{ 变量CDDate={ 秒:0, 分钟:0, 小时:0, 天数:0 } x=毫秒; CDDate.seconds=Math.roundx+30%60; x=x/60; CDDate.minutes=Math.roundx%60; x=x/60; CDDate.hours=Math.roundx%24; x=x/24; CDDate.days=Math.floorx; 返回日期; } var diff=日期-日期.now; diff=diff/1000; 设置间隔函数{ var returnVal=倒计时差; document.getElementById'low'。innerHTML=returnVal.seconds; document.getElementById'min'。innerHTML=returnVal.minutes; document.getElementById'hrs'。innerHTML=returnVal.hours; document.getElementById'day'。innerHTML=returnVal.days; },1000 秒数: 分钟: 小时: 天:
你每秒钟倒数一毫秒,需要几个小时才能看到秒和秒的变化minutes@adeneo哦,愚蠢的我:/是的,愚蠢的问题
var date = new Date(2017, 3, 27, 21).getTime();
function countdown(milliseconds) {
    console.log(milliseconds);
    var CDDate = {
        seconds: 0,
        minutes: 0,
        hours: 0,
        days: 0
    }
    x = milliseconds / 1000;
    CDDate.seconds = Math.round(x % 60);
    x /= 60;
    CDDate.minutes = Math.round(x % 60);
    x /= 60;
    CDDate.hours = Math.round(x % 25);
    x /= 24;
    CDDate.days = Math.floor(x);
    return CDDate;
}

var diff = date - Date.now();
setInterval(function(){
    console.log(countdown(--diff));
},1000)