为jquery的ajax请求添加超时timeout时间

栏目: jQuery · 发布时间: 8年前

内容简介:有时侯要用ajax来轮询某个服务是否可用,但是各个浏览器ajax的超时时间有可能不一样,所以希望ajax能只尝试几秒钟,然后隔几秒再次发送一次ajax检查一次。可以用timeout属性。

有时侯要用ajax来轮询某个服务是否可用,但是各个浏览器ajax的超时时间有可能不一样,所以希望ajax能只尝试几秒钟,然后隔几秒再次发送一次ajax检查一次。可以用timeout属性。

var checkLoading  = function(timer) {
  //先延时再获取状态,否则立即获取可能重启前的服务还没有关闭
  setTimeout(function() {
    $.ajax({
      url: '/onceos/version',
      dataType: 'json',
      timeout: 4000,
      success: function(result) {
        console.log('OK')
      },
      error: checkLoading
    })
  }, timer || 1000)
}


//第一次等10秒,第一次轮
checkLoading(10000)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Machine Learning

Machine Learning

Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00

Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具