jq--ajax中止请求
生活随笔
收集整理的這篇文章主要介紹了
jq--ajax中止请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
比如我后端設置延遲3s再響應給前端,我用的是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);//向后斷返回數據let st = await students.find();ctx.response.type = 'application/json';ctx.body = st;
}) ?
前端設置超時
<script>var url='http://localhost:3000/vueDemo/getStudents'//發起請求var getStudents=$.ajax({type: 'get',url:url,timeout:2000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過2s還未回應,中斷請求console.log('超過2s還未回應,中斷請求')getStudents.abort()}) </script>
?
?
前端設置非超時
<script>var url='http://localhost:3000/vueDemo/getStudents'//發起請求var getStudents=$.ajax({type: 'get',url:url,timeout:5000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過2s還未回應,中斷請求console.log('超過2s還未回應,中斷請求')getStudents.abort()}) </script>
?
轉載于:https://www.cnblogs.com/dshvv/p/7784415.html
總結
以上是生活随笔為你收集整理的jq--ajax中止请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [NOI 2015]荷马史诗
- 下一篇: 数据结构之算法