Timing response from prompt box ...

Discussion in 'Javascript' started by M100C, Mar 5, 2010.

  1. M100C

    M100C Guest

    My son is learning his multiplication facts, and I've built a nifty
    little browser applet that runs with javascript. I pass a randomly
    constructed math fact (e.g. "6 X 7 =") to a prompt box, and await his
    input. However, I'd like to capture how long it takes him to answer
    the prompt. I've tried something like this:

    var date = new Date()
    var time = date.getTime()
    response = prompt (fact)
    time = date.getTime() - time

    and

    var s_date = new Date()
    var e_date = new Date()
    var s_time = s_date.getTime()
    response = prompt (fact)
    var e_time = e_date.getTime()
    var time = (e_time - s_time) / 1000

    but, when I watch these variables, they are assigned the same time.
    Not sure what I am doing wrong. Any suggestions?
     
    M100C, Mar 5, 2010
    #1
    1. Advertising

  2. M100C wrote:

    > My son is learning his multiplication facts, and I've built a nifty
    > little browser applet that runs with javascript.


    Such programs are usually _not_ called applets. That term is more or less
    reserved for client-side Java (!= JavaScript) programs running in browsers.

    > I pass a randomly constructed math fact (e.g. "6 X 7 =") to a prompt box,
    > and await his input. However, I'd like to capture how long it takes him
    > to answer the prompt. I've tried something like this:
    >
    > var date = new Date()
    > var time = date.getTime()
    > response = prompt (fact)
    > time = date.getTime() - time
    >
    > and
    >
    > var s_date = new Date()
    > var e_date = new Date()
    > var s_time = s_date.getTime()
    > response = prompt (fact)
    > var e_time = e_date.getTime()
    > var time = (e_time - s_time) / 1000
    >
    > but, when I watch these variables, they are assigned the same time.
    > Not sure what I am doing wrong. Any suggestions?


    The Date instance referred to by `date' always stores the same time value,
    regardless when you call its getTime() method.

    In the first block, lose the unnecessary `time' initialization, declare
    `response', replace `prompt' with `window.prompt', replace the second call
    with `new Date()', and subtract `date'. (Implicit type conversion to
    number allows this operation to succeed, see ES3/5, section 9.3. You might
    want to choose more fitting identifiers in the process.)

    Lose the second block, and delimit your assignments with a trailing
    semicolon as recommended.


    HTH

    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Mar 6, 2010
    #2
    1. Advertising

  3. M100C

    Jorge Guest

    On Mar 6, 12:47 am, M100C <> wrote:
    > My son is learning his multiplication facts, and I've built a nifty
    > little browser applet that runs with javascript.  I pass a randomly
    > constructed math fact (e.g. "6 X 7 =") to a prompt box, and await his
    > input.  However, I'd like to capture how long it takes him to answer
    > the prompt.  I've tried something like this:
    >
    > var date = new Date()
    > var time = date.getTime()
    > response = prompt (fact)
    > time = date.getTime() - time
    >
    > and
    >
    > var s_date = new Date()
    > var e_date = new Date()
    > var s_time = s_date.getTime()
    > response = prompt (fact)
    > var e_time = e_date.getTime()
    > var time = (e_time - s_time) / 1000
    >
    > but, when I watch these variables, they are assigned the same time.
    > Not sure what I am doing wrong.  Any suggestions?


    Yes.

    The date object is not a live, running clock. Instead, it represents a
    given, fixed date and time. IOW, you need to create one before the
    prompt and another after.

    var time= +new Date();
    response = prompt(fact);
    time= (+new Date()- time) / 1e3;
    --
    Jorge.
     
    Jorge, Mar 6, 2010
    #3
  4. Jorge wrote:

    > var time= +new Date();
    > response = prompt(fact);
    > time= (+new Date()- time) / 1e3;


    The unary pluses are superfluous here, the minus already converts to number.


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Mar 6, 2010
    #4
  5. M100C

    Jorge Guest

    On Mar 6, 1:14 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > Jorge wrote:
    > > var time= +new Date();
    > > response = prompt(fact);
    > > time= (+new Date()- time) / 1e3;

    >
    > The unary pluses are superfluous here, the minus already converts to number.


    Ok. Thanks.
    --
    Jorge.
     
    Jorge, Mar 6, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gnarlodious
    Replies:
    4
    Views:
    17,608
    elizas
    May 5, 2010
  2. Replies:
    3
    Views:
    2,019
    Cousin Stanley
    Dec 31, 2003
  3. gaurav kashyap
    Replies:
    2
    Views:
    622
    gaurav kashyap
    Oct 30, 2008
  4. gaurav kashyap
    Replies:
    3
    Views:
    688
    gaurav kashyap
    Oct 31, 2008
  5. Mel
    Replies:
    10
    Views:
    3,128
    Sailaja Appi
    Feb 13, 2009
Loading...

Share This Page