欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angularjs之$timeout指令詳解

 更新時(shí)間:2017年06月13日 11:08:16   作者:流浪貓の窩  
本篇文章主要主要介紹了angularjs之$timeout指令詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

angular.js的$timeout指令對window.setTimeout做了一個(gè)封裝,它的返回值是一個(gè)promise對象.當(dāng)定義的時(shí)間到了以后,這個(gè)promise對象就會(huì)被resolve,回調(diào)函數(shù)就會(huì)被執(zhí)行.

如果需要取消一個(gè)timeout,調(diào)用$timeout.cancel(promise)方法.

用法:

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

fn: 回調(diào)函數(shù)(必填)

delay: number類型.延遲的時(shí)間(非必填),如果不填,表示等線程空下來以后就執(zhí)行.比如當(dāng)頁面被渲染完成后.

invokeApply: 布爾值.是否需要進(jìn)行臟值檢測(非必填),不填默認(rèn)為false,如果設(shè)置為true,則fn回調(diào)會(huì)被包在$scope.$apply()中執(zhí)行

返回值: 返回一個(gè)promise對象.當(dāng)定義的時(shí)間到了以后,這個(gè)promise對象就會(huì)被resolve.resolve的值就是fn回調(diào)函數(shù)的返回值

方法:

$timeout.cancel([promise])

promise: 一個(gè)由$timeout()所創(chuàng)建的promise對象.(非必填).調(diào)用cancel()以后,這個(gè)promise對象就會(huì)被reject.

返回值: 如果$timeout()的回調(diào)還沒有被執(zhí)行,那就取消成功.返回true

下面來簡單的測試一下:

   var timeoutApp = angular.module('timeoutApp',[]);
   timeoutApp.run(function($timeout){
     var a = $timeout(function(){
       console.log('執(zhí)行$timeout回調(diào)');
       return 'angular'
     },1000);
     a.then(function(data){
       console.log(data)
     },function(data){
       console.log(data)
     });
     //$timeout.cancel(a);
   })

運(yùn)行以后看到控制臺(tái)打印:

執(zhí)行$timeout回調(diào)
angular

如果我打開注釋,執(zhí)行.cancel()方法,那么$timeout的回調(diào)就不會(huì)被執(zhí)行,它返回的promise被reject,控制臺(tái)打印:

canceled

下面做個(gè)很實(shí)用的小demo: 延遲下拉菜單: 鼠標(biāo)放到button上的時(shí)候,延遲500毫秒顯示下拉菜單,當(dāng)鼠標(biāo)離開button的時(shí)候,延遲500毫秒隱藏下拉菜單,如果鼠標(biāo)是進(jìn)入了下拉菜單部分,那么就不隱藏下拉菜單.如果鼠標(biāo)離開了下拉菜單,延遲500毫秒隱藏下拉菜單,如果鼠標(biāo)是進(jìn)入了button,那么還是不隱藏下拉菜單

html:

<!DOCTYPE html>
<html ng-app="timeoutApp">
<head>
  <title>$timeout服務(wù)</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)
      };
    }
  }
})

代碼應(yīng)該很好理解: 就是進(jìn)入button和進(jìn)入ul下拉菜單的時(shí)候,都定義一個(gè)timeout回調(diào)(過500毫秒以后顯示下拉菜單),同時(shí)取消隱藏下拉菜單的回調(diào).而離開button和ul的時(shí)候相反.

代碼地址: https://github.com/OOP-Code-Bunny/angular/tree/master/%24timeout

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論