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. mac OSX10.12.4 (16E195)下Mysql 5.7.18找不到配置文件my.cnf2. mysql - 怎么生成這個sql表?3. mysql儲存json錯誤4. php - 公眾號文章底部的小程序二維碼如何統計?5. mysql - 表名稱前綴到底有啥用?6. mysql - 數據庫表中,兩個表互為外鍵參考如何解決7. Navicat for mysql 中以json格式儲存的數據存在大量反斜杠,如何去除?8. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現存在即更新應該使用哪個標簽?9. mysql - 數據庫建字段,默認值空和empty string有什么區別 11010. sql語句 - 如何在mysql中批量添加用戶?
