I'm working on a Table that logs all action and refreshes the table based on the interval given.
Everything works fine except when using setInterval
, here's my code:
var interval = 3000;
var interval_en = true;
$(document).ready(function() {
$('#logs_tbl').DataTable();
$('.paginate_button,.paginate_button.current').click(function(){
e.preventDefault();
console.log("paginate click");
interval_en = false;
}).change(function(e){
e.preventDefault();
console.log("paginate click");
interval_en = false;
});
var aclogs_interval = setInterval(function () {
if(interval_en){
console.log("logging=true");
aclog_refresh(); //This function refreshes the table via Ajax
}else{
console.log("logging stopped");
clearInterval(aclogs_interval);
}
}, interval);
} );
What have I tried so far:
Different selector:
$('#logs_tbl_paginate a').click(function(){ interval_en = false; clearInterval(aclogs_interval); });
Binding per interval:
var aclogs_interval = setInterval(function (e) { if(interval_en){ console.log("logging=true"); aclog_refresh(); $('.paginate_button,.paginate_button.current').click(function(){ e.preventDefault(); console.log("paginate click"); interval_en = false; }).change(function(e){ e.preventDefault(); console.log("paginate click"); interval_en = false; }); }else{ console.log("logging stopped"); clearInterval(aclogs_interval); } }, interval);
Though manually triggering interval_en = false;
in the browser console will stop it. Still can't find out what went wrong.
Also clicking the pagination buttons before the first interval occur works.