延迟,异步调用

官网地址:http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html

require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Deferred, dom, on){
function asyncProcess(){
var deferred = new Deferred(); dom.byId("output").innerHTML = "I'm running..."; setTimeout(function(){
deferred.resolve("success");
}, 1000); return deferred.promise;
} on(dom.byId("startButton"), "click", function(){
var process = asyncProcess();
process.then(function(results){
dom.byId("output").innerHTML = "I'm finished, and the result was: " + results;
});
}); });

链式调用

require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Deferred, dom, on){
function asyncProcess(msg){
var deferred = new Deferred(); dom.byId("output").innerHTML += "<br/>I'm running..."; setTimeout(function(){
deferred.resolve(msg);
}, 1000); return deferred.promise;
} on(dom.byId("startButton"), "click", function(){
var process = asyncProcess("first");
process.then(function(results){
dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
return asyncProcess("second");
}).then(function(results){
dom.byId("output").innerHTML += "<br/>I'm really finished now, and the result was: " + results;
});
}); });

reject

require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Deferred, dom, on){
function asyncProcess(msg){
var deferred = new Deferred(); dom.byId("output").innerHTML += "<br/>I'm running..."; setTimeout(function(){
deferred.progress("halfway");
}, 1000); setTimeout(function(){
deferred.resolve("finished");
}, 2000); setTimeout(function(){
deferred.reject("ooops");
}, 1500); return deferred.promise;
} on(dom.byId("startButton"), "click", function(){
var process = asyncProcess();
process.then(function(results){
dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
}, function(err){
dom.byId("output").innerHTML += "<br/>I errored out with: " + err;
}, function(progress){
dom.byId("output").innerHTML += "<br/>I made some progress: " + progress;
});
});

cancel

require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Deferred, dom, on){
function asyncProcess(){
var timeout;
var deferred = new Deferred(function(reason){
clearTimeout(timeout);
dom.byId("output").innerHTML += "<br/>I was cancelled with reason: " + reason;
}); dom.byId("output").innerHTML += "<br/>I'm running..."; timeout = setTimeout(function(){
dom.byId("output").innerHTML += "<br/>My process completed!";
deferred.resolve("finished");
}, 2000); return deferred.promise;
} on(dom.byId("startButton"), "click", function(){
var process = asyncProcess();
process.then(function(results){
dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
}); setTimeout(function(){
process.cancel("goodbye");
}, 1000);
}); });

dojo 官方翻译 dojo/Deferred的更多相关文章

  1. dojo 官方翻译 dojo/aspect

    官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...

  2. dojo 官方翻译 dojo/_base/lang 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang 应用加载声明: require ...

  3. dojo 官方翻译 dojo/_base/array 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html#dojo-base-array array模块dojo进行 ...

  4. dojo 官方翻译 dojo/domReady 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/domReady.html#dojo-domready dom加载完成后,执行. requi ...

  5. dojo 官方翻译 dojo/json 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/json.html#dojo-json require(["dojo/json&q ...

  6. dojo 官方翻译 dojo/string 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/string.html#dojo-string require(["dojo/st ...

  7. DOJO官方API翻译或解读-dojo/store (自定制存储器)

    dojo/store 是对已存数据的访问和存储的统一接口,dojo/store意图以一个简单.易于使用和扩展的API来,替代.集合和改善 dojo/data 和dojox/storage .基于HTM ...

  8. 现代DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...

  9. Events with Dojo(翻译)

    In this tutorial, we will be exploring dojo/on and how Dojo makes it easy to connect to DOM events. ...

随机推荐

  1. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

  2. VC++ 给你的代码强制加一个硬断点

    类似与Javascript的 debugger; Hard code a debugger breakpoint If you need to insert a hard breakpoint in ...

  3. 浅析Java与C#的事件处理机制

    http://www.cnblogs.com/OOAbooke/archive/2012/02/18/2356899.html

  4. 分享申请IDP账号的过程,包含duns申请的分享

    本文转载至 http://www.cocoachina.com/ios/20140325/8038.html 5月份接到公司要申请开发者账号的任务,就一直在各个论坛找申请的流程,但都是一些09年10年 ...

  5. [原创]Nexus5 移植OneStep

    OneStep 简介 https://github.com/SmartisanTech/android One Step 涉及的工程列表: frameworks_base (需要更改WindowMan ...

  6. vs git .vs12.suo

    GIT无法自动忽略SUO文件的解决方法 最近发现一个巨烦人的问题,项目里明明已经通过gitignore忽略了.suo文件,但是每次git pull的时候总是还得到.suo文件冲突的提示,也就是说git ...

  7. 【BZOJ2055】80人环游世界 有上下界费用流

    [BZOJ2055]80人环游世界 Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个 ...

  8. matplotlib绘制圆饼图

    import matplotlib.pyplot as plt labels = ['Nokia','Samsung','Apple','Lumia'] values = [10,30,45,15] ...

  9. phpstrrchr()函数的问题

    strrchr — 查找指定字符在字符串中的最后一次出现 说明 string strrchr ( string $haystack , mixed $needle ) 该函数返回 haystack 字 ...

  10. 微信支付 超时 mysql.event

    $wtime 使用具体timestamp //rand 防推测 $wev = 'ev_gbuy_create_' . trim($winsert_id) . rand(100, 999); $sql ...