angularjs之$timeout指令詳解
angular.js的$timeout指令對(duì)window.setTimeout做了一個(gè)封裝,它的返回值是一個(gè)promise對(duì)象.當(dāng)定義的時(shí)間到了以后,這個(gè)promise對(duì)象就會(huì)被resolve,回調(diào)函數(shù)就會(huì)被執(zhí)行.
如果需要取消一個(gè)timeout,調(diào)用$timeout.cancel(promise)方法.
用法:
$timeout(fn, [delay], [invokeApply]);
fn: 回調(diào)函數(shù)(必填)
delay: number類(lèi)型.延遲的時(shí)間(非必填),如果不填,表示等線程空下來(lái)以后就執(zhí)行.比如當(dāng)頁(yè)面被渲染完成后.
invokeApply: 布爾值.是否需要進(jìn)行臟值檢測(cè)(非必填),不填默認(rèn)為false,如果設(shè)置為true,則fn回調(diào)會(huì)被包在$scope.$apply()中執(zhí)行
返回值: 返回一個(gè)promise對(duì)象.當(dāng)定義的時(shí)間到了以后,這個(gè)promise對(duì)象就會(huì)被resolve.resolve的值就是fn回調(diào)函數(shù)的返回值
方法:
$timeout.cancel([promise])
promise: 一個(gè)由$timeout()所創(chuàng)建的promise對(duì)象.(非必填).調(diào)用cancel()以后,這個(gè)promise對(duì)象就會(huì)被reject.
返回值: 如果$timeout()的回調(diào)還沒(méi)有被執(zhí)行,那就取消成功.返回true
下面來(lái)簡(jiǎn)單的測(cè)試一下:
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
如果我打開(kāi)注釋,執(zhí)行.cancel()方法,那么$timeout的回調(diào)就不會(huì)被執(zhí)行,它返回的promise被reject,控制臺(tái)打印:
canceled
下面做個(gè)很實(shí)用的小demo: 延遲下拉菜單: 鼠標(biāo)放到button上的時(shí)候,延遲500毫秒顯示下拉菜單,當(dāng)鼠標(biāo)離開(kāi)button的時(shí)候,延遲500毫秒隱藏下拉菜單,如果鼠標(biāo)是進(jìn)入了下拉菜單部分,那么就不隱藏下拉菜單.如果鼠標(biāo)離開(kāi)了下拉菜單,延遲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)(過(guò)500毫秒以后顯示下拉菜單),同時(shí)取消隱藏下拉菜單的回調(diào).而離開(kāi)button和ul的時(shí)候相反.
代碼地址: https://github.com/OOP-Code-Bunny/angular/tree/master/%24timeout
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例
今天小編就為大家分享一篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法
這篇文章主要介紹了AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法,結(jié)合實(shí)例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下2017-06-06
angularjs實(shí)現(xiàn)過(guò)濾并替換關(guān)鍵字小功能
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)過(guò)濾并替換關(guān)鍵字小功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式
數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來(lái)操縱網(wǎng)站上存在的元素。2021-05-05
詳解Monaco?Editor中的Keybinding機(jī)制
這篇文章主要為大家介紹了詳解Monaco?Editor中的Keybinding機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Angular5升級(jí)RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解
這篇文章主要給大家介紹了關(guān)于Angular5升級(jí)RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解決方法,文中介紹了兩個(gè)解決方法,大家可以選擇使用,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-04-04
angularjs 動(dòng)態(tài)從后臺(tái)獲取下拉框的值方法
今天小編就為大家分享一篇angularjs 動(dòng)態(tài)從后臺(tái)獲取下拉框的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
總結(jié)十個(gè)Angular.js由淺入深的面試問(wèn)題
這篇文章雖然只有10個(gè)問(wèn)題,但是覆蓋了angular開(kāi)發(fā)中的各個(gè)方面,有基本的知識(shí)點(diǎn),也有在開(kāi)發(fā)過(guò)程中遇見(jiàn)的問(wèn)題,同時(shí)也有較為開(kāi)放性的問(wèn)題去辨別面試者的基礎(chǔ)水準(zhǔn)和項(xiàng)目經(jīng)驗(yàn),注意答案僅供參考哦~2016-08-08

