jQuery clearinterval不';不工作/不工作';t清除所有事件的间隔
我有一个自动刷新函数,如果选中复选框并单击按钮,就会调用该函数。它会从php文件中自动刷新一个表,并将某些变量绑定到该按钮。单击另一个按钮(绑定了不同的变量)时,自动刷新必须停止,即必须清除间隔。 然而,我无法开始工作,虽然我尝试在代码中的每一个可能的地方调用clearinterval函数,但间隔只是相加的-旧的没有被清除 我的文件是这样组成的:jQuery clearinterval不';不工作/不工作';t清除所有事件的间隔,jquery,ajax,variables,setinterval,clearinterval,Jquery,Ajax,Variables,Setinterval,Clearinterval,我有一个自动刷新函数,如果选中复选框并单击按钮,就会调用该函数。它会从php文件中自动刷新一个表,并将某些变量绑定到该按钮。单击另一个按钮(绑定了不同的变量)时,自动刷新必须停止,即必须清除间隔。 然而,我无法开始工作,虽然我尝试在代码中的每一个可能的地方调用clearinterval函数,但间隔只是相加的-旧的没有被清除 我的文件是这样组成的: $.ajaxSetup({ cache: false }); var refreshId = null; function stopint
$.ajaxSetup({
cache: false
});
var refreshId = null;
function stopinterval() {
clearInterval(refreshId);
return false;
}
$(document).ready(function() {
$("#button1").click(function(infralivefun) {
event.preventDefault(infralivefun);
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
if ($('#autorefcheck').is(':checked')) {
refreshId = setInterval(function() {
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
}, 5000);
}
//trying to call the stopinterval inside the button.Click event below
$('#button2').click(function() {
stopinterval();
});
$('#button3').click(function() {
stopinterval();
});
$('#autorefcheck').click(function() {
stopinterval();
});
}); //end of on.click(button)
//here I'm trying to call clearinterval outside of the button.click
$('#button2').click(function() {
stopinterval();
});
$('#button3').click(function() {
stopinterval();
});
$('#autorefcheck').click(function() {
stopinterval();
});
}); //end of document.ready
假设button2和button3有相似的组成,只是有其他传递给php的变量。如果有人能给我指出正确的方向,告诉我当我点击其他东西时,如何才能完全清除这个间隔,我会非常高兴 有点需要帮助
$.ajaxSetup({cache: false}); //Are you sure of it?
$(function(){
var refreshId='';
function stopinterval() {
clearInterval(refreshId);
return false;
}
$("#button1").on('click',function(infralivefun) {
event.preventDefault(infralivefun);
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
if ($('#autorefcheck').is(':checked')) {
refreshId = setInterval(function(){
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
},5000);
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
}
}
});
$('#button2, #button3, #autorefcheck').on('click',stopinterval);
});
有点需要帮助
$.ajaxSetup({cache: false}); //Are you sure of it?
$(function(){
var refreshId='';
function stopinterval() {
clearInterval(refreshId);
return false;
}
$("#button1").on('click',function(infralivefun) {
event.preventDefault(infralivefun);
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
if ($('#autorefcheck').is(':checked')) {
refreshId = setInterval(function(){
var category_id = {};
category_id['datumanf'] = $("#datumanf").datepicker().val();
category_id['datumend'] = $("#datumend").datepicker().val();
},5000);
$.ajax({
type: "POST",
url: "infratestomc.php?id=" + Math.random(),
dataType: "html",
data: category_id,
success: function(response) {
$("#resulttabelle").show().html(response);
}
});
}
}
});
$('#button2, #button3, #autorefcheck').on('click',stopinterval);
});
如果你多次点击这个按钮,你会有多个时间间隔,因为你从不检查它是否在运行
if ($('#autorefcheck').is(':checked')) {
stopinterval(); //cancel it if it is already set
refreshId = setInterval(function() {
...
在其他单击事件中添加单击事件也是一个坏主意。因为每次单击时,您都会向这些其他元素添加多个单击处理程序。如果您多次单击按钮,您将有多个间隔运行,因为您从未检查它是否正在运行
if ($('#autorefcheck').is(':checked')) {
stopinterval(); //cancel it if it is already set
refreshId = setInterval(function() {
...
在其他单击事件中添加单击事件也是一个坏主意。因为每次单击时,您都会向这些其他元素添加多个单击处理程序。我不确定这是否是问题的根源,但这肯定是一个可能的问题:您有一个全局变量保存间隔ID。 假设用户单击按钮1;存储
refreshID
,以便以后清除。然后用户再次单击按钮1(实际上是另一个按钮),然后refreshId
获得另一个(不同的)值。所以你永远无法清除第一个
避免这种情况的最简单方法是尝试在每次setInterval()调用之前清除间隔:
stopinterval();
refreshId = setInterval(...)
通常,最好将每个新间隔存储在不同的变量中,可能存储在一个全局可访问的对象中,这样您就可以根据自己的喜好管理ID。比如:
myIntervals.push({"button1": setInterval(...)});
我不确定这是否是问题的根源,但这肯定是一个可能的问题:只有一个全局变量保存区间ID。
假设用户单击按钮1;存储refreshID
,以便以后清除。然后用户再次单击按钮1(实际上是另一个按钮),然后refreshId
获得另一个(不同的)值。所以你永远无法清除第一个
避免这种情况的最简单方法是尝试在每次setInterval()调用之前清除间隔:
stopinterval();
refreshId = setInterval(...)
通常,最好将每个新间隔存储在不同的变量中,可能存储在一个全局可访问的对象中,这样您就可以根据自己的喜好管理ID。比如:
myIntervals.push({"button1": setInterval(...)});
我不知道为什么它不起作用,但是你考虑过如果用户在按钮1上点击两次会发生什么吗?您将失去对上一个间隔的引用,并且永远无法恢复。实际上,这可能是您出现问题的原因之一。你有没有在没有先清除的情况下为其他按钮调用相同的setInterval?@Aioros-hm我不太清楚我是否理解,如果单击某个按钮,我确实希望失去对以前间隔的所有引用!是的,我确实为其他按钮调用了相同的setInterval,我认为当我单击另一个按钮时它会被清除(请参阅代码的最后一部分)Ok,但是您必须在setInterval()之前调用您的stopInterval()
,否则,refreshId
将更改,您将无法清除以前设置的间隔。我不确定这是否是你的情况,但点击按钮1两次肯定会发生。@Aioros我很确定我错过了一些非常基本的东西。你能告诉我正确的方向吗?在我再次呼叫之前,我如何正确地清除间隔?我不确定它为什么不工作,但你是否考虑过如果用户在按钮1上单击两次会发生什么情况?您将失去对上一个间隔的引用,并且永远无法恢复。实际上,这可能是您出现问题的原因之一。你有没有在没有先清除的情况下为其他按钮调用相同的setInterval?@Aioros-hm我不太清楚我是否理解,如果单击某个按钮,我确实希望失去对以前间隔的所有引用!是的,我确实为其他按钮调用了相同的setInterval,我认为当我单击另一个按钮时它会被清除(请参阅代码的最后一部分)Ok,但是您必须在setInterval()之前调用您的stopInterval()
,否则,refreshId
将更改,您将无法清除以前设置的间隔。我不确定这是否是你的情况,但点击按钮1两次肯定会发生。@Aioros我很确定我错过了一些非常基本的东西。你能告诉我正确的方向吗?在我再次呼叫之前,我如何正确地清除间隔?