Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Timing response from prompt box ...

Reply
Thread Tools

Timing response from prompt box ...

 
 
M100C
Guest
Posts: n/a
 
      03-05-2010
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?
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-06-2010
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
 
Reply With Quote
 
 
 
 
Jorge
Guest
Posts: n/a
 
      03-06-2010
On Mar 6, 12:47*am, M100C <clbe...@gmail.com> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-06-2010
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$>
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      03-06-2010
On Mar 6, 1:14*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Streaming PDF, measuring response timing Ron ASP .Net 4 11-23-2009 05:35 PM
prompt to save on exit - disable prompt on save button Mel ASP .Net 10 02-13-2009 05:32 AM
How to open a shell prompt from an existing shell prompt gaurav kashyap Python 3 10-31-2008 04:41 AM
How to open a shell prompt from an existing shell prompt gaurav kashyap Python 2 10-30-2008 09:18 AM
timing a web response using urllib.urlopen?? MCollins@seminolecountyfl.gov Python 3 12-31-2003 05:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57