简体   繁体   English

显示计时器倒数

[英]Showing timer countdown

In my game I have set the timer to 30000ms (30 secs). 在我的游戏中,我将计时器设置为30000ms(30秒)。 When the timer ends the game ends. 当计时器结束时,游戏结束。 I want to show the user the timer to give them some idea of how long they have left. 我想向用户展示计时器,让他们知道他们还剩多长时间。 How would I do this, I have tried to do it like this: 我将如何执行此操作,我尝试这样做:

setTimeout(function() {
                    $("#message").html('Thank you for playing. </br> You answered </br>' + hit + '/' + attempted + ' correctly. </br> Press "Play" to start again.').fadeIn('slow');
                    $(".character").fadeIn('slow');
                }, 500);
            });
        }, 30000).show('#timer'); //here I am trying to display the time in the "#timer"
    }

I think I am approaching this all wrong can someone give me a point in the right direction? 我认为我正在解决所有错误,有人可以向我指出正确的方向吗?

Fiddle: http://jsfiddle.net/cFKHq/8/ 小提琴: http : //jsfiddle.net/cFKHq/8/

Have a gander at this jsFiddle . 对这个jsFiddle有兴趣 You just need to change your timeout to an interval and keep a count of the seconds. 您只需要将timeout更改为interval并保持秒数即可。

Instead of running the timer by all 30000 at once, run the timer in 1000ms increments. 不必一次运行所有30000个计时器,而是以1000ms为增量运行计时器。 Each time it hits, subtract one from the counter, refresh the page, and if the counter is 0, end the game. 每次命中时,从计数器中减去1,刷新页面,如果计数器为0,则结束游戏。

Here is an example: http://jsfiddle.net/RPSMJ/4/ 这是一个示例: http : //jsfiddle.net/RPSMJ/4/

JavaScript 的JavaScript

var counter = 30,
    timerOutput = document.getElementById('timerOutput');
(function timer() {
    timerOutput.innerHTML = counter;
    counter -= 1;
    if (counter >= 0) {
        setTimeout(function () {
            timer();
        }, 1000);
    }
}());​

HTML 的HTML

<div id='timerOutput'></div>​

I changed the jsfiddle you posted to show a counter after pushing play button. 按下播放按钮后,我更改了您发布的jsfiddle以显示一个计数器。 See http://jsfiddle.net/cFKHq/10/ http://jsfiddle.net/cFKHq/10/

Inside your function startplay() I added a variable ttime with an initial value of 30 and I added the line $("#timer").text(--ttime); 在您的函数startplay()我添加了一个初始值为30的变量ttime ,并添加了行$("#timer").text(--ttime); to the setInterval argument function that is called every second. 到每秒调用一次的setInterval参数函数。

Look into using delta times. 调查使用增量时间。 I have found this to be a great way to handle things that need to measure changes in time. 我发现这是处理需要衡量时间变化的事情的好方法。

prevTime = curTime;
curTime += new Date();
deltaTime = curTime - prevTime;

This will also allow you to have a "x seconds remaining" counter if you fire off an event every 1 second, or even every 100ms. 如果每隔1秒甚至每100ms触发一次事件,这也将使您有一个“剩余x秒”的计数器。 All depends on how fast you want it to go. 一切都取决于您想要多快。

You can use a global window.duration variable were you set the seconds the game will last, then decrement from that in the "one second" interval: 您可以使用全局window.duration变量,设置游戏将持续的秒数,然后在“一秒钟”间隔内将其递减:

See working demo 查看工作演示

At start of script: 在脚本开始处:

var hit = 0;
var attempted = 0;
var target = $("#target");
window.cont = true;
window.duration = 30; //set your desired game duration in seconds

then on the "one second interval" we decrement the timer and print the timer text: 然后在“一秒钟的间隔”内减少计时器并打印计时器文本:

     play = setInterval(function() {
        if (window.cont) {
            window.cont = false;
            scramble();
        }
        window.duration--;
        $('#timer').html(window.duration + ' seconds to go');
    }, 1000);

Also display a "finished" text when the game ends, the relevant part follows: 游戏结束时还显示“完成”文本,相关部分如下:

setTimeout(function() {
                    $("#message").html('Thank you for playing. </br> You answered </br>' + hit + '/' + attempted + ' correctly. </br> Press "Play" to start again.').fadeIn('slow');
                    $(".character").fadeIn('slow');
                    $('#timer').html('Finished');
                }, 500);

You need to seperate your timer logic from your "game over" logic, as the timer should tick regardless: 您需要将计时器逻辑与“游戏结束”逻辑分开,因为计时器应该打勾,无论如何:

var secondsLeft = 30;
var timerElement = $('#timer');

(function timer() {
    timerElement.text(secondsLeft);
    secondsLeft--;

    if (secondsLeft !== 0) {
        setTimeout(function () {
            timer();
        }, 1000);
    }
}());

http://jsfiddle.net/cFKHq/14/ http://jsfiddle.net/cFKHq/14/

Note that in this solution the 30 * 1 second timer countdown and 30 seconds of gameplay are not related or reliant on one another, which may cause some synchronicity issues if the user is on a slow machine. 请注意,在此解决方案中,30 * 1秒的计时器倒计时和30秒的游戏时间是不相关的或相互依赖的,如果用户使用的是慢速计算机,则可能会导致一些同步问题。

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

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