我对dojo相对较新,虽然我已经成功完成了所有需要,但有些事情我无法弄清楚如何处理它。
简单地说,我有一个表单,当我点击提交按钮时,请求很好,大部分都是好的。但是,现在我希望在div或某些内容中,每当用户等待请求通过时每10秒左右更改一次...这是我无法弄清楚的怎么办。
我想要放在div中的内容是静态的,所以不需要在另一个请求中发出请求或任何类似的疯狂,我想要的是内容每次都经常更改(例如,我想要一个div那句话说“你一直在等待X秒并计算”。)
希望有人能帮我一把吗?提前致谢
答案 0 :(得分:7)
由于其AOP方法,即Dojo.connect(),Dojo正好在哪里闪耀。以下是代码(以及jsFiddle):
dojo.require("dojox.timing");
dojo.require("dijit.form.Button");
dojo.declare("phusick.Form", null, {
send: function() {
var def = dojo.xhrPost({
url: "/echo/json/",
content: {
json: dojo.toJson({data: "some data"})
},
handleAs: "json"
});
def.addCallback(this, "onSendSuccess");
this.onSend();
},
onSend: function() {},
onSendSuccess: function(result) {}
});
dojo.declare("phusick.Observer", null, {
observe: function(form) {
this.form = form;
this.interval = 5;
this.timer = new dojox.timing.Timer(this.interval);
dojo.connect(this.timer, "onStart", this, function() {this.timeElapsed = 0});
dojo.connect(this.timer, "onTick", this, "onTick");
dojo.connect(form, "onSend", this.timer, "start");
dojo.connect(form, "onSendSuccess", this.timer, "stop");
},
onTick: function() {
this.timeElapsed += this.interval;
dojo.byId("output").innerHTML = this.timeElapsed + " ms";
}
});
dojo.ready(function() {
var form = new phusick.Form();
var observer = new phusick.Observer();
observer.observe(form);
dojo.connect(dijit.byId("sendBtn"), "onClick", function() {
form.send();
});
});
答案 1 :(得分:0)
简单的解决方案是
// 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