程序要实现的功能,ajax访问服务器成功,但服务器繁忙,(在服务器上通过Thread.sleep(10000000)方法模拟服务器阻塞),如果1分钟没有响应,前台页面做响应的处理。我试了两种方法。
1.jQuery ajax timeout参数
$.ajax({
async:false,
cache:false,
timeout:8000,
type:"POST",
url:"someurl.htm",
data:allFormValues,
error:function(jqXHR, textStatus, errorThrown){
alert("some error occurred")
alert(textStatus);
},
success:function(msg){ alert(msg); }
});
效果:ajax成功访问服务器,一直处于服务器响应中状态,用fireBug观看XHR status状态码无,一直请求中。 n久之后,返回响应,进入success. 如果把服务器down掉,进入error方法。
这些不是我想要的结果。
timeout参数api如下:
timeout
Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.
The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent。
大概意思:如果有其他请求队列(该请求在客户端被阻塞),或者没有联网,就有可能在ajax sent方法被调用之前超时。 按照其定义,timeout参数,应该只是请求超时,不涉及响应。 此处,服务器被阻塞,请求却是成功了,服务器响应中。。。。。
而且,不会执行 error和success方法,直至响应完毕,或者服务器down掉(也算响应完毕)
2.window.setTimeout方法
本来用此法,思路如下:
function click(){
var flag=0;
setTimeout(aa,5000);
function aa(){
//to do something
if(flag==0) {
alert("timeout");
window.reload();
}
}
$.ajax({
async;false,
cache:false,
timeout:8000,
type:"POST",
url:"someurl.htm",
data:allFormValues,
error:function(jqXHR, textStatus, errorThrown){
flag=1;
alert("some error occurred")
alert(textStatus);
},
success:function(msg){
flag=1;
alert(msg);
}
});
}
预期效果:给ajax 5秒的时间,如果没响应(包括success和error),则做进一步处理。弹出timeout,刷新本页,干掉ajax.
在网上查找 setTimeout方法确实是模拟了另开一个线程(不是真的?求解)
但效果:
setTimeout 5秒钟后并没有执行aa函数, 而是。。
ajax无限等待 response,等n久后或故意把服务端down掉,先进入ajax error回调或success回调函数, 又过5秒,在执行aa
晕了,为什么?是把整个function执行完,在执行setTimeout?
坐等各位遇到此问题的朋友来交流,大牛们给予指点。
问题已解决:把 $.ajax({
async:false,
cache:false,
timeout:8000,
type:"POST",
url:"someurl.htm",
data:allFormValues,
error:function(jqXHR, textStatus, errorThrown){
alert("some error occurred")
alert(textStatus);
},
success:function(msg){ alert(msg); }
});
设为 async:true,设为异步,
可以了,8秒后返回弹出"some error occurred",then alert "timeout"
参数设置为同步,timeout参数貌似不管用............
分享到:
相关推荐
JavaScript_window.setTimeout()_的详细用法
这是我们常常会使用方法setInterval或setTimeout,但是由于这两个方法是由脚本宿主模拟出来的Timer线程,在通过其调用我们的方法是不能为其传递参数。 我们常用的使用场景是: 代码如下:window.setTimeout(...
一个React钩子,提供window.setTimeout的版本,该版本在卸载时会自行清除。 消除了不必担心在 React 组件中手动清除卸载超时的开销。 特征 提供可以像window.setTimeout一样调用的回调。 调用时,返回一个可以调用...
虽然我学习和应用javascript已经两年多了,但是对setTimeout方法,有时候也要查阅资料。今天对js的setTimeout方法做一个系统地总结。 setInterval与setTimeout的区别 说道setTimeout,很容易就会想到setInterval,...
其语法是: window.setTimeout(expr,msec) expr是执行字符串,在msec毫秒之后,就会作为js运行。我昨天才发现,原来expr也可以是一个函数,呵呵,用这个特性,就可以进行对象的传送了。 下面的代码实现了把函数foo1...
fd-超时 window.setTimeout FP 风格安装npm install fd-timeout --save 用法 var timeout = require ( 'fd-timeout' ) ;var after2s = timeout ( 2000 ) ;after2s ( function ( ) { alert ( 'FP rocks' ) ;}...
和window.clearTimeout一起使用.我在 代码如下:$(document).ready(function(){setTimout(test(),200); function test() { alert(1); } }); 只会执行一次,有朋友说可以使用 代码如下:setInterval (...
精解window.setTimeout()&window.setInterval()使用方式与参数传递问题!
js中settimeout方法加参数的使用实例.docx
下面给大家介绍为jquery的ajax请求添加超时timeout时间的实例 有时侯要用ajax来轮询某个服务是否可用,但是各个浏览器ajax的超时时间有可能不一样,所以希望ajax能只尝试几秒钟,然后隔几秒再次发送一次ajax检查一次...
window对象提供了两个方法来实现定时器的效果,分别是window.setTimeout()和window.setInterval。 在Javascript中,代码一般都是同步执行的,但定时器却是异步执行的。 window.setTimeout(callback,delay); //...
有关动机和方法的更多,请参阅。 用法 零超时作为 ESM 导入提供。 import 'zero-timeout' ; 导入模块将两个方法添加到全局范围。 设置零超时 这个方法的接口就像但没有delay参数: var timeoutID = window . ...
/****************************************************** // // 功能: 修改window.setTimeout,使之可以传递参数和对象参数 // 使用方法: window.setTimeout(回调函数,延迟时间,参数1,参数n) // ******************...
jquery.contip.js 是一个基于JQuery的气泡提示框tip插件。轻量级,扁平化,不用加载CSS文件,自定义风格,事件监听,代码注释清晰,可扩展性强。 //示例代码: var pop = $('.elm').contip({ align: '...
3)window.setTimeout(函数名称,时间)//延时定时器,执行一次 4)window.clearTimeout(定时器的返回值);停止定时器 2:document.getElementById() 功能:获取特定ID元素的节点 参数:ID的属性名 返回值:dom...
window.settimeout()方法要调用带参数的函数有两种方法: 1. 代码如下: function init(){ var url = “<%=basePath%>fetchwater.do?method=searchRealWater&xzqh=” + “<%=xzqh%>” + “&rand=”+...
一、解释说明 1、概述 setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段 ... timeoutID 是该延时操作的数字ID, 此ID随后可以用来作为window.clearTimeout方法的参数 func 是你想
本文实例讲述了js实现window.open不被拦截的解决方法。分享给大家供大家参考。具体分析如下: 一、问题: 今天在处理页面ajax请求过程中,想实现请求后打开新页面,就想到通过 js window.open 来实现,但是最终都被...
js中settimeout方法加参数.docx