jq--ajax中止请求
生活随笔
收集整理的這篇文章主要介紹了
jq--ajax中止请求
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
比如我后端設(shè)置延遲3s再響應(yīng)給前端,我用的是node之koa2
router.get('/vueDemo/getStudents', async ( ctx ) => {//延遲3sasync function delay(time) {return new Promise(function(resolve, reject) {setTimeout(function(){resolve();}, time);});};await delay(3000);//向后斷返回數(shù)據(jù)let st = await students.find();ctx.response.type = 'application/json';ctx.body = st;
}) ?
前端設(shè)置超時
<script>var url='http://localhost:3000/vueDemo/getStudents'//發(fā)起請求var getStudents=$.ajax({type: 'get',url:url,timeout:2000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過2s還未回應(yīng),中斷請求console.log('超過2s還未回應(yīng),中斷請求')getStudents.abort()}) </script>
?
?
前端設(shè)置非超時
<script>var url='http://localhost:3000/vueDemo/getStudents'//發(fā)起請求var getStudents=$.ajax({type: 'get',url:url,timeout:5000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過2s還未回應(yīng),中斷請求console.log('超過2s還未回應(yīng),中斷請求')getStudents.abort()}) </script>
?
轉(zhuǎn)載于:https://www.cnblogs.com/dshvv/p/7784415.html
總結(jié)
以上是生活随笔為你收集整理的jq--ajax中止请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [NOI 2015]荷马史诗
- 下一篇: 求一个真心喂了狗的个性签名