2017-05-26 62 views
0

我想清除按鈕點擊的時間間隔,但它沒有清除間隔,我也嘗試過全局範圍。這裏是我的功能,設置間隔:一個按鈕,我想clearintervalclearInterval()不清除間隔

/* 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> 
+0

在嘗試按下該按鈕之前,window.interval的值是多少? – Booster2ooo

+0

嘗試替換警報機智console.log並用'window.interval'替換'interval' –

+0

您的間隔時間是什麼時候設置的?我不認爲你設置了時間間隔,所以你可以清除它。嘗試使用斷點(調試器),並在嘗試清除時確定是否定義了間隔。 –

回答

0

也是一個面向對象的方法可以幫助你以更好地控制您的示波器:

system = { 
    interval :false, 
    showSaveWorkDialog: function(action, blueprint_id){ 
    // ... 
    system.interval && clearInterval(system.interval); 
    system.interval = setInterval(function() { 
     //... 
    }, 1000); 
    }, 
    clickLink: function(){ 
    system.interval && clearInterval(system.interval); 
    system.interval=false; 
    } 
} 


<a onclick="system.clickLink()" ... > 
+0

感謝@john它爲我工作:) –

+0

很高興它幫助;-)堅持這種做法,它的優雅! –

0

我不知道問題出在哪裏從何而來,我們可能需要一個完整的工作樣本模仿這個問題。

不過,怎麼樣在JS處理您的活動,你可以在你的範圍更好的控制:

(function($) { 
    $(document).ready(function() { 
     var interval; 
     function showSaveWorkDialog(action, blueprint_id) { 
      alert('page Lost Access'); 
      interval && clearInterval(interval); // avoid the previous interval to continue "unhandled" 
      interval = setInterval(function() { 
       console.log("still going"); 
       //... 
      }, 1000); 
     }; 

     $('body').on('click','a.clear-interval', function(e) { 
      console.log(interval) 
      interval && clearInterval(interval); 
      interval = 0; 
      return false; 
     }); 
    }); 
}(jQuery)); 

和鏈接:

<a class="clear-interval" data-dismiss="modal"><i class="fa fa-check" aria-hidden="true"></i> OK</a> 
+0

我已經編輯了完整代碼的問題 –

+0

你確定'onHiddenCallback','CheckOtherUserWantsAccess'和'showSaveWorkDialog'被調用嗎? – Booster2ooo

+0

在不檢查interval的值的情況下調用'clearInterval(interval)'沒有什麼壞處 - ''clearInterval()'只會忽略先前已被清除的'undefined'或者間隔ID。但是,OP在調用'setInterval()'之前應該調用'clearInterval()'。 – nnnnnn

0

當您創建間隔時,完整的功能在內部編寫,但是當你嘗試清除它時,需要同樣的東西。 嘗試witht下一個系統:

var interval = setInterval(logtext,1000); 

function logtext(){ 
    console.log("Its working"); 
} 

然後清除它只是使用

clearInterval(interval); 

另一個系統,我喜歡使用:

var interval = null; 

    function startinterval(){ 
     if (!interval) { 
     interval = setInterval(logtext,1000); 
     } 
    }; 
    function stopinterval(){ 
     if (interval) { 
     clearInterval(interval); 
     interval = null; 
     } 
    }; 

然後只需使用:

startinteval(); 
stopinterval(); 
+0

'stopInterval()'在'stopInterval()'後面再次調用'startInterval()'不會重新創建一個新的時間間隔,因爲你沒有重置'interval'變量。 – nnnnnn

+0

我忘了它,現在改變了 – CristianS9