标签:style blog color os io java strong ar for
jquery deferred是一个异步回调的机制
<textarea id="document"></textarea> <p id="indicator"></p> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> //创建一个deferred对象 var defer = $.Deferred(); var wordGoal = 30; //defer挂起时的callback defer.progress(function (wordCount) { var percentComplete = Math.floor(wordCount/wordGoal*100); $(‘#indicator‘).text(percentComplete + ‘%complete‘); }); //defer完成时的callback defer.done(function(msg) { $(‘#indicator‘).text(msg); }); //事件回调 $(‘#document‘).on(‘keypress‘, function() { var wordCount = $(this).val().split(/\s+/).length; //回调中监听完成事件并触发 if (wordCount >= wordGoal) { defer.resolve(‘Good job!‘); } //回调中监听挂起事件并处罚 defer.notify(wordCount); }); </script>
*一旦执行了defer对象,再做defer的notify将不会有任何反应,就像任何额外的resolve调用及reject调用也会被无视一样
标签:style blog color os io java strong ar for
原文地址:http://www.cnblogs.com/cjy1993/p/3951174.html