user962206
user962206

Reputation: 16137

Implementing a timer app in Metro App

I am developing a game in Metro app where there would be an initial timer for the game to run, let's say about 1 minute and 50 seconds and the timer is displaying the current time. if the 1 minute and 50 seconds time is over, the game will be over it will show a message, How would I implement such behaviour?

Upvotes: 0

Views: 348

Answers (3)

Adam Lear
Adam Lear

Reputation: 38778

This basically does the trick, though this is the only thing my app does, so your real performance might vary and there are likely other improvements, stylistic and otherwise, you can make:

var timer = setInterval(function () {
    var div = document.getElementById('time'); // this is just a div

    // the div shows the time like this: "1:20" - 1 minute, 20 seconds
    var lastValue = div.innerText.split(':');

    var newValue = parseInt(lastValue[0]) * 60 + parseInt(lastValue[1]) + 1;

    if (newValue === 110) {
        div.innerText = "Game over!";
        clearInterval(timer);
    } else {
        div.innerText = Math.floor(newValue / 60) + ':' + newValue % 60;
    }
}, 1000);

For something more robust, check out this article. It looks like it's pretty close to what you want to do.

Upvotes: 1

Annie
Annie

Reputation: 3190

I would say, you can try this (untested):

remainingTime = 110;
setInterval(function() {
        countdownStarted(); // game started
    }, 
    milliseconds // game will start in this much time
);

function countdownStarted() {
    setInterval(function() {
            remainingTime = remainingTime*100;
            updateTimeOnScreen(); // after every second
            if(remainingTime) countdownStarted();
        },
        100
    );
}

function updateTimeOnScreen() {
    if(remainingTime == 0) {
        timeUp(); // game over
    }
    // function continues
}

For more examples, I would suggest you to read this article.

Upvotes: 1

Mamta D
Mamta D

Reputation: 6460

You can create an object that has a setTimeout and clearTimeout methods set to some user-defined value.

This link can also give you some useful info.

Upvotes: 0

Related Questions