我遇到点击多次触发功能的问题。具体来说,在下面的代码中,userloggedin();单击时,函数会多次触发。通常情况下,它会在第一次触发,下次触发3次,下次触发6次。
var timer = null;
function sessCountdown() {
var sec = 120;
timer = setInterval(function() {
sec--;
if (sec >= 0) {
span = document.getElementById("count");
span.innerHTML = sec;
}
}, 1000);
}
function getUserInfo() {
var getUserInfo = $.ajax({
type: 'Get',
url: Config.engineUrl + '?AUserSessionInfo',
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true
});
getUserInfo.done(function(data, jqXHR) {
var sessionModal = $('#session-modal');
if (data.TimeLeft > 0 && data.TimeLeft <= 2 && !sessionModal.hasClass('in')) {
sessionModal.modal('show');
sessCountdown();
} else if (data.TimeLeft === 0){
$('.logout').trigger('click');
}
sessionModal.on('shown.bs.modal', function(e) {
$('#btnSession').on('click', function() {
clearInterval(timer);
userLoggedin();
});
});
});
}
//Check for session info every 60 seconds
setInterval(getUserInfo, 60000);
我感谢您提供解决此问题的任何提示。感谢。
更新的代码 - 使用分辨率
var timer = null;
function sessCountdown() {
var sec = 120;
timer = setInterval(function() {
sec--;
if (sec >= 0) {
span = document.getElementById("count");
span.innerHTML = sec;
}
}, 1000);
}
function getUserInfo() {
var sessionModal = $('#session-modal'),
getUserInfo = $.ajax({
type: 'Get',
url: Config.engineUrl + '?AUserSessionInfo',
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true
});
getUserInfo.done(function(data, jqXHR) {
if (data.TimeLeft > 0 && data.TimeLeft <= 2 && !sessionModal.hasClass('in')) {
sessionModal.modal('show');
sessCountdown();
} else if (data.TimeLeft === 0) {
$('.logout').trigger('click');
}
});
}
$('#btnSession').on('click', function() {
clearInterval(timer);
userLoggedin();
});
//Check for session info every 60 seconds
setInterval(getUserInfo, 60000);