我想清除按鈕點擊的時間間隔,但它沒有清除間隔,我也嘗試過全局範圍。這裏是我的功能,設置間隔:一個按鈕,我想clearinterval
的clearInterval()不清除間隔
/* call idle js */
var awayCallback = function() {
window.userLog = new Date().getTime();
CheckOtherUserWantsAccess();
console.log(new Date().toTimeString() + ": away");
};
var awayBackCallback = function() {
window.userLog = new Date().getTime();
console.log(new Date().toTimeString() + ": back");
};
var onVisibleCallback = function() {
window.userLog = new Date().getTime();
console.log(new Date().toTimeString() + ": now looking at page");
};
var onHiddenCallback = function() {
window.userLog = new Date().getTime();
CheckOtherUserWantsAccess();
console.log(new Date().toTimeString() + ": on hidden callback");
};
//this is another way of using it
var idle = new Idle({
onHidden: onHiddenCallback,
onVisible: onVisibleCallback,
onAway: awayCallback,
onAwayBack: awayBackCallback,
awayTimeout: 120000 //away with 120 seconds of inactivity
}).start();
console.log(idle);
function CheckOtherUserWantsAccess() {
var url = window.location.href;
var array = url.split("/");
var reversed_array = array.reverse();
if(window.location.href.indexOf("qualification") > -1) {
var action = reversed_array[2];
}
else
{
var action = reversed_array[0];
}
if (jQuery.inArray(action, pageArray) !== -1) {
var blueprint_id = '';
if ($("#lock_blueprint_id").length) {
blueprint_id = $("#lock_blueprint_id").attr("class");
}
if (typeof (window.pageLockMsg !== undefined) && window.pageLockMsg === "Page locked") {
} else {
$.ajax({
type: "post",
data: {check_action: action, blueprint_id: blueprint_id},
url: "/pagelock/checkotheruserwantaccess",
success: function (res) {
if (res == 'save your work') {
showSaveWorkDialog(action, blueprint_id);
} else {
// console.log('<--------- continue working ---------->');
}
}
});
}
}
}
/* Set flag to true for want page access by locked user */
setTimeout(function() {
console.log("set timeout");
if (typeof (window.pageLockMsg !== undefined) && window.pageLockMsg === "Page locked") {
var url = window.location.href;
var array = url.split("/");
var reversed_array = array.reverse();
if(window.location.href.indexOf("qualification") > -1) {
var action = reversed_array[2];
}
else
{
var action = reversed_array[0];
}
if (jQuery.inArray(action, pageArray) !== -1) {
var blueprint_id = '';
if ($("#lock_blueprint_id").length) {
blueprint_id = $("#lock_blueprint_id").attr("class");
}
$.ajax({
type: "post",
data: {check_action: action, blueprint_id: blueprint_id},
url: "/pagelock/wantaccess",
success: function (res) {
// console.log(res);
}
});
}
}
}, 5000);
/* show save work popup */
function showSaveWorkDialog(action, blueprint_id) {
alert('page Lost Access');
$('#saveWorkDialog').modal({
backdrop: 'static',
keyboard: false
});
var n = $('.timespan-20').attr('id');
var c = n;
$('.timespan-20').text(c);
interval = setInterval(function() {
console.log("still going");
c--;
if (c >= 0) {
$('.timespan-20').text(c);
}
if (c == 0) {
/* remove the access for the user */
$.ajax({
type: "post",
data: {check_action: action, blueprint_id: blueprint_id},
url: "/pagelock/deleteuseraccess",
success: function (res) {
$(".abc").addClass('masterTooltip_right');
$(".abc").find("a").attr('rel', '#pagelockDialog');
$("input").attr('disabled', 'disabled');
$("textarea").attr('disabled', 'disabled');
$(".save-btn").css('display', 'none');
$('.masterTooltip_right').hover(function() {
// Hover over code
var title = $(this).attr('title');
$(this).data('tipText', title).removeAttr('title');
$('<p class="tooltip4"></p>')
.text(title)
.appendTo('body')
.fadeIn('slow');
}, function() {
// Hover out code
$(this).attr('title', $(this).data('tipText'));
$('.tooltip4').remove();
}).mousemove(function (e) {
var mousex = e.pageX - 30; //Get X coordinates
var mousey = e.pageY + 10; //Get Y coordinates
$('.tooltip4')
.css({top: mousey, left: mousex})
});
window.pageLockMsg = "Page locked";
$("#saveWorkDialog").find(".warning-locking-content").html('<a><img alt="warning" src="/images/warning-logo.png"></a><p style="margin:-41px 0 0 110px;">Your access has been lost.</p>');
$(".save").attr("title", "Your access has been lost.")
console.log('your access is revoked.');
}
});
}
}, 1000);
}
/* Update the pageaccess time for the user who has read/write access */
setInterval(function() {
console.log("set interval");
if (typeof (window.pageLockMsg === undefined) && window.pageLockMsg !== "Page locked") {
var url = window.location.href;
var array = url.split("/");
var reversed_array = array.reverse();
if(window.location.href.indexOf("qualification") > -1) {
var action = reversed_array[2];
}
else
{
var action = reversed_array[0];
}
if (jQuery.inArray(action, pageArray) !== -1) {
var blueprint_id = '';
if ($("#lock_blueprint_id").length) {
blueprint_id = $("#lock_blueprint_id").attr("class");
}
$.ajax({
type: "post",
data: {check_action: action, blueprint_id: blueprint_id},
url: "/pagelock/updatepageaccesstime",
success: function (res) {
}
});
}
}
}, 30000);
的onclick:
<a onclick="clearInterval(interval)" data-dismiss="modal"><i class="fa fa-check" aria-hidden="true"></i> OK</a>
在嘗試按下該按鈕之前,window.interval的值是多少? – Booster2ooo
嘗試替換警報機智console.log並用'window.interval'替換'interval' –
您的間隔時間是什麼時候設置的?我不認爲你設置了時間間隔,所以你可以清除它。嘗試使用斷點(調試器),並在嘗試清除時確定是否定義了間隔。 –