如何做一些事情,而一个道场XHR请求正在等待或负载 [英] How to do something while a dojo xhr request is waiting or loading

查看:113
本文介绍了如何做一些事情,而一个道场XHR请求正在等待或负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的道场,虽然我已经成功地做​​我的需要的,有一些事情我只是无法弄清楚如何处理它。

简单地说,我有一个表格,当我点击提交按钮请求进入细和在大多数情况下都是很好的。不过,现在我的需要的有在一个div或改变每10秒某些内容或因此而用户一直等待的请求要经过...这就是我想不通该怎么办。

我想放在格的内容是静态的,所以没有必要再要一个请求或类似的任何疯狂内部的请求,我要的是,每隔一段时间内容的变化(例如说我想一个div ,说:你一直在等待X秒和计数)。

希望任何人都可以给我一个手呢?在此先感谢

解决方案

简单的解决方案将是

  1. 消防的要求
  2. 在启动的setInterval做你的东西,或显示装载机GIF或任何
  3. 当请求返回,撤消在第2部分做了什么。

  // 1
VAR REQ = dojo.xhr(/*...*/);

// 2
VAR跨=的setInterval(函数(){
    的console.log(等待...); //做的东西
},10000); //时间以毫秒为单位

// 3
功能撤消(){
    clearInterval(间);
}
req.then(撤销,撤销); //撤消无论是在sucess或错误
 

I'm relatively new to dojo, and although I've managed to do all I need, there are certain things I just can't figure out how to do with it.

Simply put, I have a form and when I click the submit button the request goes in fine and for the most part all is good. However, now I want to have in a div or some content that changes every 10 seconds or so while the user been waits for the request to go through... this is what I can't figure out how to do.

The content I want to put in the div is static, so there's no need to have a request inside another request or any madness like that, all I want is that every so often the content changes (eg say I want a div that says "You've been waiting X seconds and counting").

Hopefully anybody can give me a hand with this? Thanks in advance

解决方案

The simple solution would be

  1. Fire a request
  2. Start a setInterval to do your stuff or show a loader gif or whatever
  3. When the request returns, undo what you did in part 2.


// 1
var req = dojo.xhr( /*...*/ );

// 2 
var inter = setInterval(function(){
    console.log("waiting..."); //do stuff
}, 10000); //time in miliseconds

//3
function undo(){
    clearInterval(inter);
} 
req.then(undo, undo); //undo either on sucess or on error

这篇关于如何做一些事情,而一个道场XHR请求正在等待或负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆