简体   繁体   English

带有JavaScript的实时表单倒数计时器

[英]Real-time form countdown timer with javascript

Pardon my grammar/spelling 请原谅我的语法/拼写

So, I was searching about how to make a countdown timer with javascript, but I want it to be in <form> , so when the user typed some numbers (which became the seconds), and then they click "Start", the timer will be started. 因此,我正在搜索如何使用JavaScript 制作倒数计时器 ,但我希望它使用<form> ,因此,当用户键入一些数字(即秒),然后他们单击“开始”时,计时器将开始。 But, I stuck in developing the script. 但是,我坚持开发脚本。

Here's the body: 这是身体:

 $('#detik').keyup(function() { var sec = $(this).val(); }); var display = document.querySelector('#time'), timer = new CountDownTimer(sec), timeObj = CountDownTimer.parse(sec); format(timeObj.minutes, timeObj.seconds); timer.onTick(format); $('#timerb').click(function() { timer.start(); }); function format(minutes, seconds) { minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.textContent = minutes + ':' + seconds; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/robbmj/simple-js-countdown-timer/master/countdowntimer.js"></script> <div class="text-center"> <h1 id="time"></h1> </div> <div class="text-center"> <form id="formtimer" name="formtimer" class="form-inline" action="#" method="post"> <input type="number" class="form-control" id="detik" placeholder="How many secs?"> <button class="btn btn-default" type="submit" id="timerb">Start!</button> </form> </div> 

What's wrong with my script? 我的脚本怎么了? Why I can't show the 00:00 ? 为什么我不能显示00:00

Look this: 看看这个:

 $(function(){ $('#timerb').click(function() { var sec = $("#detik").val(); var timer = new CountDownTimer(sec); var timeObj = CountDownTimer.parse(sec); format(timeObj.minutes, timeObj.seconds); timer.onTick(format); timer.start(); }); function format(minutes, seconds) { minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; var display = document.querySelector('#time'); display.textContent = minutes + ':' + seconds; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/robbmj/simple-js-countdown-timer/master/countdowntimer.js"></script> <div class="text-center"> <h1 id="time"></h1> </div> <div class="text-center"> <form id="formtimer" name="formtimer" class="form-inline" action="#" method="post"> <input type="number" class="form-control" id="detik" placeholder="How many secs?"> <button class="btn btn-default" type="button" id="timerb">Start!</button> </form> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM