gpt4 book ai didi

javascript - AngularJS Ionic $timeout 显示分秒

转载 作者:行者123 更新时间:2023-11-29 18:01:36 25 4
gpt4 key购买 nike

我正在制作一个倒计时应用程序,我想显示倒计时结束前的分钟和秒数。你有什么想法?我用 $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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com