Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.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-如何每秒更新变量_Javascript_Setinterval - Fatal编程技术网

JAVASCRIPT-如何每秒更新变量

JAVASCRIPT-如何每秒更新变量,javascript,setinterval,Javascript,Setinterval,我需要比较开始时间和当前时间,但我不知道如何使当前时间始终刷新到我比较的变量。 我为编辑道歉,在以前的解决方案中,我错误地描述了错误 var timeout = $payuEle.attr("data-ajax-timeout"); // VERIFICATION OF TIMEOUT TIME // ////////////////////////////////// // Creates a new start date

我需要比较开始时间和当前时间,但我不知道如何使当前时间始终刷新到我比较的变量。
我为编辑道歉,在以前的解决方案中,我错误地描述了错误

        var timeout = $payuEle.attr("data-ajax-timeout");

        // VERIFICATION OF TIMEOUT TIME //
        //////////////////////////////////

        // Creates a new start date
        var startTime = new Date();

        // Converts the start date to milliseconds
        var startTimeInMilliseconds = startTime.getTime();

        // Converts seconds from the 'timeout' attribute to milliseconds
        var secondsInMilliseconds = timeout * 1000;

        // Adds milliseconds of start date + 'timeout' = time when authentication expires
        var endTimeInMilliseconds = startTimeInMilliseconds + secondsInMilliseconds;

        // Converts milliseconds of end time to date (functionally not redeemed, only for testing purposes in console)
        var endTime = new Date(endTimeInMilliseconds);

        // Predefined variable, which then saves the current time
        var readyForActualTime = "";

        // A variable calling a function for the current time
        var actualTimeStore = getActualTime(readyForActualTime);
        var actualTimeStoreInMilliseconds = actualTimeStore.getTime();

        // Rounds the last two milliseconds to avoid minor variations
        var endTimeCompare = Math.round(endTimeInMilliseconds/100)*100;
        var startTimeCompare = Math.round(actualTimeStoreInMilliseconds/100)*100;

        console.log(startTime, endTime);

        // A function that creates the current time
        function getActualTime(ocekavanyParametr) {
            // Creates the current time
            var actualTime = new Date();
            // Returns current time to variable ''
            return actualTime;
        }

        // It restores function every second to keep the actual time
        setInterval(getActualTime, 1000);

        // Compare times
        if (endTimeCompare === startTimeCompare) {
            alert('Its a match!');
        }

感谢您的帮助

您只需调用间隔一次,因为
实际时间
在函数中。为了更清楚地看到它,如果删除变量并记录
new Date()

函数logActualTime(){
log('现在是:',新日期());
}

设置间隔(logActualTime,1000)
您只需要调用一次interval,因为
actualTime
在函数中。为了更清楚地看到它,如果删除变量并记录
new Date()

函数logActualTime(){
log('现在是:',新日期());
}

设置间隔(logActualTime,1000)您可能想要这个。你把事情复杂化了很多,你需要在循环中创造时间

var timeout=5;//秒
//超时时间的验证//
//////////////////////////////////
//创建新的开始日期
var startTime=新日期();
开始时间。设置毫秒(0);//正常化
//将开始日期转换为毫秒
var starttimeinmillseconds=startTime.getTime();
//将秒从“timeout”属性转换为毫秒
var secondsinmicrosides=超时*1000;
//添加开始日期的毫秒数+“超时”=身份验证过期时的时间
var endtimeinmillizes=starttimeinmillizes+secondsinmillizes;
//它每秒恢复一次功能以保持实际时间
var tId=设置间隔(getActualTime,1000);
//创建当前时间的函数
函数getActualTime(){
//创建当前时间
var actualTime=新日期();
actualTime.setmillizes(0);//正常化
actualTimeInMilliseconds=actualTime.getTime();
log(新日期(endtimein毫秒),新日期(actualtimeinmillseconds));
//比较时间
如果(EndTimeInMillistics===ActualTimeInMillistics){
清除超时(tId)
log('it a match!');
}

}
您可能想要这个。你把事情复杂化了很多,你需要在循环中创造时间

var timeout=5;//秒
//超时时间的验证//
//////////////////////////////////
//创建新的开始日期
var startTime=新日期();
开始时间。设置毫秒(0);//正常化
//将开始日期转换为毫秒
var starttimeinmillseconds=startTime.getTime();
//将秒从“timeout”属性转换为毫秒
var secondsinmicrosides=超时*1000;
//添加开始日期的毫秒数+“超时”=身份验证过期时的时间
var endtimeinmillizes=starttimeinmillizes+secondsinmillizes;
//它每秒恢复一次功能以保持实际时间
var tId=设置间隔(getActualTime,1000);
//创建当前时间的函数
函数getActualTime(){
//创建当前时间
var actualTime=新日期();
actualTime.setmillizes(0);//正常化
actualTimeInMilliseconds=actualTime.getTime();
log(新日期(endtimein毫秒),新日期(actualtimeinmillseconds));
//比较时间
如果(EndTimeInMillistics===ActualTimeInMillistics){
清除超时(tId)
log('it a match!');
}

}
为什么说ActualTime会神奇地改变,仅仅因为你不止一次地将它记录在console.log中?你需要
console.log(getActualTime())
在你的时间间隔内,或者保持
setInterval(getActualTime,1000)
…或者您需要
getActualTime
来更新
sayActualTime
。目前,它只是在每个时间间隔将值返回为虚无。你是一个天才mplungjan,谢谢你的帮助,我只是一个初学者,为这个愚蠢的问题感到抱歉…没关系,我看到了更糟的:)我为编辑道歉,在以前的解决方案中,我错误地描述了错误。你能帮我解决这个问题吗@mplungjan为什么仅仅因为您多次使用console.log,就会说ActualTime神奇地改变?你需要
console.log(getActualTime())
在你的时间间隔内,或者保持
setInterval(getActualTime,1000)
…或者您需要
getActualTime
来更新
sayActualTime
。目前,它只是在每个时间间隔将值返回为虚无。你是一个天才mplungjan,谢谢你的帮助,我只是一个初学者,为这个愚蠢的问题感到抱歉…没关系,我看到了更糟的:)我为编辑道歉,在以前的解决方案中,我错误地描述了错误。你能帮我解决这个问题吗@姆普隆詹