Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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_Asp.net Mvc_Countdowntimer_Session Timeout_Timertask - Fatal编程技术网

在调用JavaScript函数之前清除标签,该函数通过相同的函数[会话超时]显示计时器

在调用JavaScript函数之前清除标签,该函数通过相同的函数[会话超时]显示计时器,javascript,asp.net-mvc,countdowntimer,session-timeout,timertask,Javascript,Asp.net Mvc,Countdowntimer,Session Timeout,Timertask,我尝试使用以下函数在标签中的网页上显示计时器(标签id为MsgTimer)。但是,当函数被调用2次或更多次时,将显示2次或更多计时器,它不会重置计时器,而是重载标签文本,并在标签上显示多个计时器。我想在每次调用函数时重置标签文本 function startTimer() { var x; clearInterval(x); document.getElementById("MsgTimer").innerHTML = ""; // Here I was supp

我尝试使用以下函数在标签中的网页上显示计时器(标签id为MsgTimer)。但是,当函数被调用2次或更多次时,将显示2次或更多计时器,它不会重置计时器,而是重载标签文本,并在标签上显示多个计时器。我想在每次调用函数时重置标签文本

function startTimer() {
    var x;
    clearInterval(x);
    document.getElementById("MsgTimer").innerHTML = "";
    // Here I was supposed to fetch the SessionTimeout data from appsettings.json 
    // But for now I entered data manually for 15 minutes
    var countDownDate = new Date().getTime() + 900000;
    // Update the count down every 1 second              
    x = setInterval(function () {        
        // Get todays date and time
        var now = new Date().getTime();
        // Find the distance between now and the count down date
        var distance = countDownDate - now;
        // Time calculations for days, hours, minutes and seconds
        var hours = Math.floor(distance / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // add a zero in front of numbers<10
        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        hours = checkTime(hours);
        minutes = checkTime(minutes);
        seconds = checkTime(seconds);


        // Display the result in the element with id="lblTime"
        document.getElementById("MsgTimer").innerHTML = "";
        document.getElementById("MsgTimer").innerHTML = "Session Expires In" + " " + minutes + " : " + seconds + "";

        // If the count down is finished, write some text
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("MsgTimer").innerHTML = "SESSION EXPIRED";
            window.alert("Session Timeout");
        }                     
    }, 1000);
}
startTimer();
函数startTimer(){
var x;
净间隔(x);
document.getElementById(“MsgTimer”).innerHTML=“”;
//这里我应该从appsettings.json获取SessionTimeout数据
//但现在我手动输入数据15分钟
var countDownDate=新日期().getTime()+900000;
//每1秒更新一次倒计时
x=setInterval(函数(){
//获取今天的日期和时间
var now=new Date().getTime();
//找出现在和倒计时日期之间的距离
var距离=倒计时日期-现在;
//天、小时、分钟和秒的时间计算
var小时=数学楼层(距离/(1000*60*60));
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);

//在数字前面加一个零代码的问题可能是
var x;
声明,因为使用
var
关键字声明的变量实际上是函数作用域

因此,每次调用
startTimer();
时,它仅在函数范围内创建一个新的
x
变量,因此
clearInterval(x)
无法清除上一个间隔,因为它无法从上一次
startTimer();
调用访问
x
的上一个值

尝试将您的
var x;
声明移到函数外部,看看它是否有效。

更新的[Working]:

/* Timer - Display Session Timeout */
var x = 0;
function startTimer() {        
    var countDownDate = (new Date().getTime()) + ((parseInt(@SessionTimer)) * 60000);
    clearInterval(x);
    x = setInterval(function () {
        var now = new Date().getTime();
        var distance = countDownDate - now;
        var hours = Math.floor(distance / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        hours = checkTime(hours);
        minutes = checkTime(minutes);
        seconds = checkTime(seconds);
        document.getElementById("MsgTimer").innerHTML = "Session Expires In" + " " + hours + " : " + minutes + " : " + seconds + "";
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("MsgTimer").innerHTML = "SESSION EXPIRED";
            window.alert("Session Timeout");
        }
    }, 1000);
}
startTimer();    
/*计时器-显示会话超时*/
var x=0;
函数startTimer(){
var countDownDate=(new Date().getTime())+((parseInt(@SessionTimer))*60000);
净间隔(x);
x=设置间隔(函数(){
var now=new Date().getTime();
var距离=倒计时日期-现在;
var小时=数学楼层(距离/(1000*60*60));
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
功能检查时间(i){
如果(i<10){
i=“0”+i;
}
返回i;
}
小时=检查时间(小时);
分钟=检查时间(分钟);
秒=检查时间(秒);
document.getElementById(“MsgTimer”).innerHTML=“会话将在“+”+小时+”:“+分钟+”:“+秒+”)后过期;
如果(距离<0){
净间隔(x);
document.getElementById(“MsgTimer”).innerHTML=“会话已过期”;
窗口警报(“会话超时”);
}
}, 1000);
}
startTimer();
备选方案[JQuery]:

/* Show 15 Minutes Timer */
var timer2 = "15:01";
var interval = setInterval(function () {
    var timer = timer2.split(':');
    //by parsing integer, I avoid all extra string processing
    var minutes = parseInt(timer[0], 10);
    var seconds = parseInt(timer[1], 10);
    --seconds;
    minutes = (seconds < 0) ? --minutes : minutes;
    if (minutes < 0) clearInterval(interval);
    seconds = (seconds < 0) ? 59 : seconds;
    seconds = (seconds < 10) ? '0' + seconds : seconds;
    //minutes = (minutes < 10) ?  minutes : minutes;
    $('.countdown').html('Session Expires In' + ' ' + minutes + ':' + seconds);
    timer2 = minutes + ':' + seconds;
}, 1000);
/*显示15分钟计时器*/
var timer2=“15:01”;
var interval=setInterval(函数(){
var timer=timer2.split(“:”);
//通过解析整数,我避免了所有额外的字符串处理
var minutes=parseInt(计时器[0],10);
var seconds=parseInt(计时器[1],10);
--秒;
分钟=(秒<0)?--分钟:分钟;
如果(分钟<0)清除间隔(间隔);
秒=(秒<0)?59:秒;
秒=(秒<10)?“0”+秒:秒;
//分钟=(分钟<10)?分钟:分钟;
$('.countdown').html('会话在'+''+分钟+'':'+秒后过期);
timer2=分钟+':'+秒;
}, 1000);

Where是
x
定义的,Where是
clearInterval
定义的?
x
在第5行定义,并在第7行分配一个函数
setInterval
,而
clearInterval
是javascript的内置函数,它应该清除
setInterval
数据。
x
是在p之后定义的在
clearInterval(x)
感谢您的回复,我只是尝试了您的建议,但无法解决我的问题。感谢您的帮助。这可能会有所帮助