angularjs之$timeout指令詳解
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)文章
angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例
今天小編就為大家分享一篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法
這篇文章主要介紹了AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法,結(jié)合實(shí)例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下2017-06-06angularjs實(shí)現(xiàn)過濾并替換關(guān)鍵字小功能
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)過濾并替換關(guān)鍵字小功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式
數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來操縱網(wǎng)站上存在的元素。2021-05-05詳解Monaco?Editor中的Keybinding機(jī)制
這篇文章主要為大家介紹了詳解Monaco?Editor中的Keybinding機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Angular5升級RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解
這篇文章主要給大家介紹了關(guān)于Angular5升級RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解決方法,文中介紹了兩個(gè)解決方法,大家可以選擇使用,需要的朋友可以參考借鑒,下面來一起看看吧。2018-04-04angularjs 動(dòng)態(tài)從后臺(tái)獲取下拉框的值方法
今天小編就為大家分享一篇angularjs 動(dòng)態(tài)從后臺(tái)獲取下拉框的值方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08總結(jié)十個(gè)Angular.js由淺入深的面試問題
這篇文章雖然只有10個(gè)問題,但是覆蓋了angular開發(fā)中的各個(gè)方面,有基本的知識(shí)點(diǎn),也有在開發(fā)過程中遇見的問題,同時(shí)也有較為開放性的問題去辨別面試者的基礎(chǔ)水準(zhǔn)和項(xiàng)目經(jīng)驗(yàn),注意答案僅供參考哦~2016-08-08