作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个倒计时应用程序,我想显示倒计时结束前的分钟和秒数。你有什么想法?我用 $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();
}
});
})
最佳答案
您可以使用$interval
进行倒计时。例如。在您的 timerCountdown
函数中开始间隔,并在回调函数(每秒调用一次)中减少剩余的秒数。
一旦秒数降为零,显示弹出消息。
Controller :
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一个工作示例。
关于javascript - AngularJS Ionic $timeout 显示分秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532934/
我在将 time.Time 转换为 Deciseconds 时遇到问题。我基本上必须将以下内容移植到 Golang Deciseconds 适合 36 位,具有足够的精度来记录用户的同意操作时间。 J
我真的需要帮助。编码结果为2:0:0,格式设置为hh:mm:ss。我希望结果为 2:00:00(当分秒小于 10 时,在分秒前添加 0)。 NSDateFormatter *test = [[NSDa
我是一名优秀的程序员,十分优秀!