当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angularjs之$timeout指令详解

angularjs之$timeout指令详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

angular.js的$timeout指令对window.settimeout做了一个封装,它的返回值是一个promise对象.当定义的时间到了以后,这个promise对象就会被resolve,回调函数就会被执行.

如果需要取消一个timeout,调用$timeout.cancel(promise)方法.

用法:

$timeout(fn, [delay], [invokeapply]);

fn: 回调函数(必填)

delay: number类型.延迟的时间(非必填),如果不填,表示等线程空下来以后就执行.比如当页面被渲染完成后.

invokeapply: 布尔值.是否需要进行脏值检测(非必填),不填默认为false,如果设置为true,则fn回调会被包在$scope.$apply()中执行

返回值: 返回一个promise对象.当定义的时间到了以后,这个promise对象就会被resolve.resolve的值就是fn回调函数的返回值

方法:

$timeout.cancel([promise])

promise: 一个由$timeout()所创建的promise对象.(非必填).调用cancel()以后,这个promise对象就会被reject.

返回值: 如果$timeout()的回调还没有被执行,那就取消成功.返回true

下面来简单的测试一下:

   var timeoutapp = angular.module('timeoutapp',[]);
   timeoutapp.run(function($timeout){
     var a = $timeout(function(){
       console.log('执行$timeout回调');
       return 'angular'
     },1000);
     a.then(function(data){
       console.log(data)
     },function(data){
       console.log(data)
     });
     //$timeout.cancel(a);
   })

运行以后看到控制台打印:

执行$timeout回调
angular

如果我打开注释,执行.cancel()方法,那么$timeout的回调就不会被执行,它返回的promise被reject,控制台打印:

canceled

下面做个很实用的小demo: 延迟下拉菜单: 鼠标放到button上的时候,延迟500毫秒显示下拉菜单,当鼠标离开button的时候,延迟500毫秒隐藏下拉菜单,如果鼠标是进入了下拉菜单部分,那么就不隐藏下拉菜单.如果鼠标离开了下拉菜单,延迟500毫秒隐藏下拉菜单,如果鼠标是进入了button,那么还是不隐藏下拉菜单

html:

<!doctype html>
<html ng-app="timeoutapp">
<head>
  <title>$timeout服务</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../bootstrap.css" rel="external nofollow" />
  <script src="../angular.js"></script>
  <script src="script.js"></script>
  <style type="text/css">
  * {
   font-family:'microsoft yahei'
  }
  </style>
</head>
<body >

 <div ng-controller="myctrl">
   <div class="dropdown" dropdown >
     <button class="btn btn-default dropdown-toggle" type="button" ng-mouseenter = "showmenu()" ng-mouseleave = "hidemenu()">
       dropdown
       <span class="caret"></span>
     </button>
     <ul class="dropdown-menu" ng-show="ifshowmenu" ng-mouseenter = "showmenu()" ng-mouseleave = "hidemenu()">
       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >action</a></li>
       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >another action</a></li>
       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >something else here</a></li>
       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >separated link</a></li>
     </ul>
   </div>
 </div>

</body>
</html>

js: 

var timeoutapp = angular.module('timeoutapp',[]);
timeoutapp.controller('myctrl',function($scope){
  $scope.ifshowmenu = false;
});
timeoutapp.directive('dropdown',function($timeout){
  return {
    restrict:"ea",
    link:function(scope,iele,iattr){
      scope.showmenu = function(){
        $timeout.cancel(scope.t2);
        scope.t1 = $timeout(function(){
          scope.ifshowmenu = true
        },500)
      };
      scope.hidemenu = function(){
        $timeout.cancel(scope.t1);
        scope.t2 = $timeout(function(){
          scope.ifshowmenu = false
        },500)
      };
    }
  }
})

代码应该很好理解: 就是进入button和进入ul下拉菜单的时候,都定义一个timeout回调(过500毫秒以后显示下拉菜单),同时取消隐藏下拉菜单的回调.而离开button和ul的时候相反.

代码地址: https://github.com/oop-code-bunny/angular/tree/master/%24timeout

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网