[英]stop ajax jquery load on submit
I have a jquery ex like this.. 我有一个这样的jQuery前..
jQuery(document).ready(function() {
var refreshId = setInterval(function() {
jQuery("#results").load('random.php?randval='+ Math.random()+'&hours='+jQuery('#hours').val()+'&mins='+jQuery('#mins').val()+'&seconds='+jQuery('#seconds').val());
jQuery("#time_spent").val(jQuery("#results").html());
}, 1000);
});
and a submit button like this 和这样的提交按钮
<input type="submit" name="submit" value="Stop" id="submit" />
Now if the submit button is clicked the ajax jquery load should be stopped atonce since the time interval is 1 second. 现在,如果单击提交按钮,则由于时间间隔为1秒,因此应立即停止ajax jQuery加载。
How to do . 怎么做 。 please help thanks Haan
请帮助谢谢哈恩
It sounds like you're doing an Ajax refresh on a timer, but when a form is submitted, you want to cancel the refreshing. 听起来您正在按计时器进行Ajax刷新,但是提交表单后,您想取消刷新。 If that's the case, when your form is submitted, you'll want to use
clearInterval()
to cancel the timer. 在这种情况下,提交表单时,您将需要使用
clearInterval()
取消计时器。
$('form').submit(function(){
clearInterval(refreshId);
});
Or you might be able to get away with doing this right in the submit button, depending on the scope of refreshId
. 或者,您可能可以在
refreshId
按钮中完成此操作,具体取决于refreshId
的范围。
<input onclick="clearInterval(refreshId);return(true);" type="submit" name="submit" value="Stop" id="submit" />
If you want to prevent #result from being updated after submitting the form, you should use a callback from the get request and check if you should update #result before putting the ajax result into it. 如果要防止在提交表单后更新#result,则应使用get请求中的回调,并在将ajax结果放入其中之前检查是否应更新#result。
var refreshId = -1;
jQuery(document).ready(function() {
var stop = false;
refreshId = setInterval(function() {
var data = {
randval: Math.random(),
hours: jQuery('#hours').val(),
mins: jQuery('#mins').val(),
seconds: jQuery('#seconds').val()
};
jQuery.get('random.php', data, function(data){
if (!stop) {
jQuery("#results").html(data);
jQuery("#time_spent").val(data);
};
});
}, 1000);
jQuery('form').submit(function(){
stop = true;
clearInterval(refreshId);
});
}); });
How about checking if some variable is true before doing some ajax stuff. 在做一些ajax之前如何检查某个变量是否为真。
Javascript: 使用Javascript:
<script type="text/javascript">
function stopWorking()
{
running = false;
}
$(document).ready(function()
{
running = true;
var refreshId = setInterval(function() {
if(running!=false)
{
jQuery("#results").load('random.php?randval='+ Math.random()+'&hours='+jQuery'#hours').val()+'&mins='+jQuery('#mins').val()+'&seconds='+jQuery('#seconds').val());
jQuery("#time_spent").val(jQuery("#results").html());
}, 1000);
});
</script>
HTML: HTML:
<input type="submit" onclick="stopWorking();return(false);" />
After clicking the submit button variable 'running' is set to false ,and all jquery actions are stopped. 单击提交按钮后,变量'running'设置为false,所有jquery动作均停止。
I am going to have a wild stab in the dark at what you are asking for. 我要在黑暗中刺破你的要求。 So if the ajax request takes more than 1 second it should be killed?
因此,如果ajax请求花费的时间超过1秒,则应将其杀死?
Try setting a timeout using jQuery's $.ajax()
function . 尝试使用jQuery的
$.ajax()
函数设置超时时间。
You need to use a different ajax method - like $.get or $.post etc... because these methods return an XMLHttpRequest object that can be stored in a variable, and later used to abort the request. 您需要使用其他ajax方法-例如$ .get或$ .post等...,因为这些方法返回一个XMLHttpRequest对象,该对象可以存储在变量中,然后用于中止请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.