欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

angularjs之$timeout指令詳解

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

弓長嶺網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),弓長嶺網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為弓長嶺近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的弓長嶺做網(wǎng)站的公司定做!

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

用法:

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

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

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

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

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

方法:

$timeout.cancel([promise])

promise: 一個由$timeout()所創(chuàng)建的promise對象.(非必填).調(diào)用cancel()以后,這個promise對象就會被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);
   })

運行以后看到控制臺打印:

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

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

canceled

下面做個很實用的小demo: 延遲下拉菜單: 鼠標放到button上的時候,延遲500毫秒顯示下拉菜單,當鼠標離開button的時候,延遲500毫秒隱藏下拉菜單,如果鼠標是進入了下拉菜單部分,那么就不隱藏下拉菜單.如果鼠標離開了下拉菜單,延遲500毫秒隱藏下拉菜單,如果鼠標是進入了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)
      };
    }
  }
})

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

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當前標題:angularjs之$timeout指令詳解
轉(zhuǎn)載注明:http://chinadenli.net/article42/jggjhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google移動網(wǎng)站建設(shè)標簽優(yōu)化企業(yè)建站搜索引擎優(yōu)化網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管