How to count time?

I explain, i'm working on a game that give you points if you choose the correct answer to a question. Now the points are always the same but i want to give points depending of how fast is the user answering. Any idea of "counting time" and transforming into points (my english is so bad... sorry)


Answer this question

How to count time?

  • rpreston

    Sure -- you can use ECMAScript's built-in Date object to do that.

    When you ask a question, remember the time you did it:

    // startTime must not be a local variable; it needs to persist
    startTime = new Date();

    Then when you get the answer from the user, get the current time:

    var now = new Date();

    Then get the difference, in seconds:

    var diffInSeconds = (now.valueOf() - startTime.valueOf()) / 1000



  • How to count time?