白天有雨

此处应该有掌声

导航

统计

公告

混合使用Jquery Deferred和Angularjs的$timeout(转)

原文链接:http://my.oschina.net/gejiawen0913/blog/174826

<!DOCTYPE html>
<html ng-app="Demo">
    <head>
        <meta charset="utf-8" />
        <title>AngularJS</title>
    </head>
    <body>
 
        <div ng-controller="TestCtrl">
            <span ng-click="go()">{{ a }}</span>
        </div>
 
        <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
 
        <script type="text/javascript">
            var app = angular.module('Demo', [], angular.noop);
            app.controller('TestCtrl', function($scope, $timeout) {
                $scope.a = '点击我开始';
 
                var defer = $.Deferred();
                var f = function() {
                    if ($scope.a == '') {
                        $scope.a = '已停止';
                        return;
                    }
                    defer.done(function() {
                        $scope.a.length < 10 ? $scope.a += '>' : $scope.a = '>';
                        $timeout(f, 100);
                    });
                };
                defer.done(function() {
                    $scope.a = '>';
                    f();
                });
 
                $scope.go = function() {
                    defer.resolve();
                    $timeout(function() {
                        $scope.a = '';
                    }, 5000);
                };
            });
        </script>
    </body>
</html>

 

posted on 2015-03-03 15:41 卓如 阅读(...) 评论(...) 编辑 收藏