如何将倒数计时器更改为分钟:秒
How to change the countdown timer into minutes : seconds?
我正在这样设计游戏,但倒数计时器仍然以秒为单位。所以我想把它改成分钟:秒。如何将其更改为分钟:秒?请给我一个解释。
//thanks to GameAlchemist
function createCountDown(timeRemaining) {
var startTime = Date.now();
return function() {
return timeRemaining - ( Date.now() - startTime );
}
}
var currentCountDown = createCountDown(30000);
// Draw everything
var render = function () {
var countDownValue = currentCountDown();
returnKillsNeeded(stageNum);
ctx.drawImage(startGameImg, 0,0);
ctx.font = "24px Helvetica";
ctx.textAlign = 'center'
ctx.textBaseline = "top";
ctx.fillStyle="#FF0000";
ctx.fillText("press Enter to play", 250, 450);
ctx.fill();
if(gameStart){
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);
}
ctx.fillStyle="#522900";
ctx.fillRect(0,480,500,120);
ctx.drawImage(scoreImg, 22,522);
ctx.drawImage(livesImg, 360,522);
ctx.drawImage(progressImg, 200,492);
createProgressBar();
createProgressPercent();
ctx.fillText("progress", 170,492);
setEnemyHealthText();
drawPlayer();
if(countDownValue <=0){
countDownValue = 0;
}else{
ctx.fillText(countDownValue, 200,190);
}
1 分钟是 60 秒,对吧?
将countDownValue
除以 60 得到分钟,然后向下舍入 fe。 用Math.floor()
使其干净。模数countDownValue
60 以获得秒。
var minutes = Math.floor(countDownValue/60);
var seconds = countDownValue%60;
然后像MM:SS一样打印时间
if(seconds > 9)
ctx.fillText(minutes + ":" + seconds, 200,190);
else
ctx.fillText(minutes + ":0" + seconds, 200,190);
这个 if 语句始终将时间打印为 7:06,而不是 7:6
将米利斯转换为分钟和秒
if(countDownValue <=0){
countDownValue = 0;
}else{
ctx.fillText(
(countDownValue/1000/60) << 0 +
':' +
(countDownValue/1000) % 60, 200, 190
);
}
替换:
ctx.fillText(countDownValue, 200,190);
由:
// Convert miliseconds to seconds.
var seconds = Math.floor(countDownValue/1000);
// A minute is 60 seconds. See how many times 60 fits in the number of seconds:
var minutes = Math.floor(seconds / 60);
// Calculate how many seconds are left when removing those minutes:
seconds -= minutes * 60;
// Format the seconds to always have 2 digits
seconds = seconds.toFixed(2);
// Output the result:
ctx.fillText(minutes + ':' + seconds, 200, 190);
一些评论:
如果使用插件适合您,请不要重新发明轮子。例如:jQuery Countdown。
您提供的代码具有不平衡的大括号。当然,这应该在代码运行之前修复。
相关文章:
- Javascript倒计时计时器倒计时未正确更新秒/分钟
- 如何在不活动5分钟后更改页面位置
- 每小时倒计时一次,但以30分钟为单位
- 角度在60多分钟内路线问题
- 计时器在使用 jQuery 一分钟后没有触发,或者每 1 分钟后给出一些其他解决方案来触发功能
- 制作一个按钮倒计时计时器,该计时器将在5分钟后永久禁用,即使在页面刷新时也应保持禁用状态
- 如何在javascript中将秒转换为分钟和小时
- 如何使用javascript将秒转换为分钟.
- 如何将分钟格式化为小时和分钟,但仅显示小时(如果's>60分钟
- 当转换为Python日期时间时,Node的时间戳落后3分钟
- 引导时间选择器在增加或减少小时和分钟的同时获取 NaN
- Moment.js unix时间戳,以分钟为单位显示以前的时间
- AngularJS-检测到n分钟内没有任何操作并进入默认状态
- 将音频第二次转换为分钟和秒格式
- 如何将倒数计时器更改为分钟:秒
- 我想要一个警报框,在倒数计时器中使用 javascript 的 10 分钟
- 如何添加提交表单的 5 分钟脚本倒数计时器
- Javasacript倒数计时器,以天,小时,分钟,秒为单位
- 将分钟或秒添加到 Javascript 倒数计时器
- JS倒数计时器与天:小时:分钟