Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在满足一定条件后,如何停止AJAX调用中的设置超时功能?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 在满足一定条件后,如何停止AJAX调用中的设置超时功能?

Javascript 在满足一定条件后,如何停止AJAX调用中的设置超时功能?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有下面一段代码,工作正常,但我想在满足某些条件时停止自动刷新 enter code here $(document).ready(function() { setTimeout( function(){ $.ajax({ url: '/jrt/?Id=$data.jacket.id', method: "GET", cache: false, success: function(data) { //$("

我有下面一段代码,工作正常,但我想在满足某些条件时停止自动刷新

enter code here
              $(document).ready(function() {


    setTimeout( function(){  $.ajax({
    url: '/jrt/?Id=$data.jacket.id',
    method: "GET",
    cache: false,
    success: function(data) {
        //$("#gt").append(data);
        $( '#gt' ).html( data );
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
    }
})
   },10000);


   });

使用stop clearTimeout停止setTimeOut函数。请参阅此部分,其中有一个很好的解释

在变量中分配setTimeout函数,并在任何情况下清除它

var timer;

$(document).ready(function() {
  timer = window.setTimeout(function() {
    $.ajax({
      url: '/jrt/?Id=$data.jacket.id',
      method: "GET",
      cache: false,
      success: function(data) {
        //$("#gt").append(data);

        $('#gt').html(data);

        if (1) // your any condition
        {
          window.clearTimeout(timer);
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
      }
    })
  }, 10000);
});
可能重复的
var timer;

$(document).ready(function() {
  timer = window.setTimeout(function() {
    $.ajax({
      url: '/jrt/?Id=$data.jacket.id',
      method: "GET",
      cache: false,
      success: function(data) {
        //$("#gt").append(data);

        $('#gt').html(data);

        if (1) // your any condition
        {
          window.clearTimeout(timer);
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
      }
    })
  }, 10000);
});