首页 > 代码库 > 关于deferred

关于deferred

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>Deffered</title>        <script type="text/javascript" src="jquery-1.11.0.js"></script>        <script>             //参考链接:http://javascript.ruanyifeng.com/jquery/deferred.html             //http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html             //http://api.jquery.com/category/deferred-object/             // 四个状态,执行中,已完成,失败了,成败皆可             // 两个需求,多个异步完成一起处理;异步同步化链式处理             // 异步同步化链式处理            var wait = function(dtd) {                var dtd = $.Deferred();                var tasks = function() {                    console.log("执行完毕");                    dtd.resolve("+1");                    //                    dtd.reject("-1");                };                setInterval(function() {                    dtd.notify("0");                }, 200);                setTimeout(tasks, 5000);                return dtd.promise();            };            $.when(wait()).then(function(x) {                console.log("成功回调" + x);                var dtd = $.Deferred();                var tasks = function() {                    dtd.resolve("+2");                    //                    dtd.reject("-2");                };                setTimeout(tasks, 3000);                return dtd.promise();            }, function(x) {                console.log("失败回调" + x);            }, function(x) {                console.log("操作还没有完成" + x);            }).then(function(x) {                console.log("再次成功回调" + x);            }).always(function() {                console.log("成功与否总是回调这个函数 ");            });        </script>    </head>    <body>    </body></html>

 

关于deferred