AngularJS Ionic $ timeout显示分钟和秒

时间:2015-12-30 16:06:06

标签: javascript angularjs

我正在制作一个倒计时应用,我想显示倒计时结束前的分钟和秒数。你有什么想法? 我用$interval做了这件事,希望它甚至可以做到这一点:D 我对AngularJS很陌生,希望有人可以帮助我。

编辑:你现在开心吗? :^)

的index.html

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-assertive">
        <h1 class="title">Pizza Timer</h1>
      </ion-header-bar>
        <ion-content class="padding" ng-controller="PopupCtrl">
          <div id="TimerStart">
            <!-- Button To Start the Timer -->
            <button class="button button-full button-assertive" ng-click="timerCountdown()">Start The Timer!</button>
            <!-- Shows the time -->
          </div>
          <div ng-app ng-controller="PopupCtrl">
            +++ TIMER SHOULD BE DISPLAYED HERE ++++
          </div>
      </ion-content>
    </ion-pane>
  </body>
</html>

app.js

    // Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var app = angular.module('starter', ['ionic'])

app.controller('PopupCtrl', function($scope, $ionicPopup, $timeout) {

$scope.format = 'mm:ss';

var stop;


// shows the alert when timer is finished
$scope.showAlert = function() {
     var alertPopup = $ionicPopup.alert({
       title: 'Your Pizza Is Ready!',
       template: 'Bon Appétit!'
     });

     alertPopup.then(function(res) {
       console.log('Pizza Is Ready!');
     });
   };

// countdown line 31 = time 
  $scope.timerCountdown = function(res){
    console.log('Timer is running!');
    var endtimer = $timeout(
      function() {
        var alertPopup = $ionicPopup.alert({
         title: 'Your Pizza Is Ready!',
         template: 'Bon Appétit!'});
      },
      2000);
  };
});

app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

1 个答案:

答案 0 :(得分:4)

您可以使用$interval倒计时。例如。在你的timerCountdown函数中启动时间间隔,并在回调函数(每秒调用一次)中,减少剩余的秒数。

一旦秒数降至零,显示弹出消息。

<强>控制器

angular.module('app', ['ionic'])

.controller('countCtrl', function countController($scope, $interval, $ionicPopup){
    $scope.countDown = 0; // number of seconds remaining
    var stop;

    $scope.timerCountdown  = function(){
      // set number of seconds until the pizza is ready
      $scope.countDown = 10;

      // start the countdown
      stop = $interval(function() {
        // decrement remaining seconds
        $scope.countDown--;
        // if zero, stop $interval and show the popup
        if ($scope.countDown === 0){
          $interval.cancel(stop);
          var alertPopup = $ionicPopup.alert({
             title: 'Your Pizza Is Ready!',
             template: 'Bon Appétit!'});
        }
      },1000,0); // invoke every 1 second
    };
});

<强>标记

<body ng-app="app">
  <ion-pane ng-controller="countCtrl">
    <ion-content class="padding">
      <button class="button" ng-click="timerCountdown ()">Start countdown</button>
      <!-- show only if countdown is running -->
      <div ng-show="countDown>0">Your pizza is ready in {{countDown}} seconds.</div>
    </ion-content>
  </ion-pane>
</body>

See here是一个有效的例子。