离子倒计时器应用程序

问题描述 投票:2回答:1

我用离子编程倒计时应用程序,我在AngularJS中不是很好,因此,我不知道我做错了什么:/也许你可以看看我的代码,它应该有一个按钮,它开始倒计时,如果倒计时结束后,会弹出一条消息。

timer.js

    angular.module('PopupTimer', [])

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

    $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!');
     });
   };

    $scope.timerCountdown = function(res){
      var alertPopup = $ionicPopup.alert({
       title: 'Your Pizza Is Ready!',
       template: 'Bon Appétit!'});

      var endtimer = $timeout([200]);

      if(endtimer == ([0])) {
       return showAlert();
      }
  }
});

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'
angular.module('starter', ['ionic'])

.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();
}


 });
})

index.html

    <!DOCTYPE html>
<html ng-app="PopupTimer">
  <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>
    <script src="js/timer.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar align-title="center" class="bar-assertive">
        <h1 class="title">Pizza Timer</h1>
      </ion-header-bar>
      <ion-content ng-controller="PopupCtrl">
        <button class="button button-full button-light" ng-click="timerCountdown">
          Alert
        </button>
      </ion-content>    
    </ion-pane>
  </body>
</html>
javascript angularjs ionic
1个回答
2
投票

如果你看一下documentation of $timeout,你会发现它的第一个参数是一个函数,应该在指定的延迟之后调用它。

延迟(第二个参数)以毫秒为单位指定。因此,要在5秒后显示警报,您的$sope.timerCountdown函数将如下所示:

$scope.timerCountdown = function(res){
  var endtimer = $timeout(
    function() {
      var alertPopup = $ionicPopup.alert({
       title: 'Your Pizza Is Ready!',
       template: 'Bon Appétit!'});
    },
    5000);
};
© www.soinside.com 2019 - 2024. All rights reserved.