angular.js - angular $interval怎么在一個按鈕上實現開始定時和結束定時
問題描述
我代碼邏輯如下,想要實現:
點擊查詢按鈕,開啟定時,按鈕文案變成停止查詢,
點擊停止查詢,取消定時
$scope.submitRequest = function () { if ($scope.onlyButton == '查詢') { $scope.onlyButton = '停止查詢'; $interval(function(){ //具體方法},5000); } else{ setTimeout(function(){ $scope.onlyButton = '查詢'; $interval.cancel(stop); },0)}; }
但是沒有成功取消定時,求幫忙...
問題解答
回答1:LZ的代碼完全可以在外面做一個 狀態標示, 例如:
var interval;$scope.startStatus = false;$scope.submitRequest = function(){ if( !$scope.startStatus ){interval = $interval(...); } else {$interval.cancel(interval); }$scope.startStatus = !$scope.startStatus; }
然后在VIEW中直接類似使用:
<button ng-bind=' !startStatus ? ’查詢’ : ’停止查詢’ '></button>
我覺得這樣寫好些~
相關文章:
1. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?2. docker start -a dockername 老是卡住,什么情況?3. docker容器呢SSH為什么連不通呢?4. Android listview checkbox 單選5. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””6. Android TextView 或 ListView 加載過渡效果7. 爬蟲圖片 - 關于Python 爬蟲的問題8. python - thrift 返回 TSocket read 0 bytes 求助!!!!9. Python列表或者字典里面的中文如何處理?10. python - (初學者)代碼運行不起來,求指導,謝謝!
