<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>jquery promise</title>
</head>
<body>
  <div id="app">
    <div id="result"></div>
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>
    $('#result').html('waiting data...');

      var promise = wait();
      promise.done(result);

      function result() {
        $('#result').html('done');
      }

      function wait() {
        var deferred = $.Deferred();

        setTimeout(function() {
          deferred.resolve();
        }, 2000);

        return deferred.promise();
      }  
  </script>
</body>
</html>

 

posted on 2017-04-25 09:05  百里登风  阅读(237)  评论(0编辑  收藏  举报