By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

javascript timers doubt

P: n/a
Hi All,

1 ) I am using the Date() function to get the current time. But it
doesnt tell me the time value in millisec. Is there any API in
javascript that returns the current time value with resolution in
millisec

2) Also i see the setTimeout value does not get expired at the correct
time. I see some delay there. Is there any way to avoid this.
3)When the javascript timer callback is getting executed, will there be
a context switching that can happen without finishing the
timercallback..I have added timestamp to the start and end of my timer
callback. But it doesnt give me a constant response for the same
operation. Sometimes it takes 1 sec, and sometime it takes around 6
sec..

Thanks

Aug 8 '05 #1
Share this Question
Share on Google+
7 Replies


P: 4
1. Use DateObject.getTime() and .getMilliseconds() method

2. setTimeout() do not return expired time - this function set "system timer" to given timeout. Using JavaScript for "realtime" may be wrong.

3. JavaScript - browser-inside interpreter. Results may be different with same code at different browsers.
Aug 8 '05 #2

P: n/a
va*********@gmail.com wrote:
Hi All,

1 ) I am using the Date() function to get the current time. But it
doesnt tell me the time value in millisec. Is there any API in
javascript that returns the current time value with resolution in
millisec
Have a browse of the stuff here:

<URL:http://www.merlyn.demon.co.uk/js-date0.htm#TaI>

and here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm#SDT>

There is also somewhere there a spot that tells you the update
interval of the internal clock - the Windows (XP & 2000) machines I
use tend to update at 15 or 16 millisecond intervals, Mac OS at 1 ms
intervals.

2) Also i see the setTimeout value does not get expired at the correct
time. I see some delay there. Is there any way to avoid this.
3)When the javascript timer callback is getting executed, will there be
a context switching that can happen without finishing the
timercallback..I have added timestamp to the start and end of my timer
callback. But it doesnt give me a constant response for the same
operation. Sometimes it takes 1 sec, and sometime it takes around 6
sec..


It may be important (or not) that setTimeout and setInterval are not
part of ECMAScript Language but are provided by the browser window
object.

--
Rob
Aug 8 '05 #3

P: n/a
Hi,
Thanks for your reply. I am not clear of the context switching. While
in the timer callback,can a context switching happen without completing
the callback.?

Aug 8 '05 #4

P: n/a
JRS: In article <42f71210$0$16486$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Mon, 8 Aug 2005 18:04:31, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted :

<URL:http://www.merlyn.demon.co.uk/js-dates.htm#SDT>

There is also somewhere there a spot that tells you the update
interval of the internal clock - the Windows (XP & 2000) machines I
use tend to update at 15 or 16 millisecond intervals, Mac OS at 1 ms
intervals.


Now
<URL:http://www.merlyn.demon.co.uk/js-dates.htm#Ress> or scan for RobG .

I'd be pleased to have interval and resolution data for other (common)
systems.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 8 '05 #5

P: n/a
Dr John Stockton wrote:
[...]

Now
<URL:http://www.merlyn.demon.co.uk/js-dates.htm#Ress> or scan for RobG .

I'd be pleased to have interval and resolution data for other (common)
systems.


OK, here's some results. Mac OS 9 was done in classic mode under OS
X, I couldn't get results from Communicator (version 4.7) it just kept
crashing. I may update the version and try again later.

System Browser Update Resolution

Mac OS X Safari 1.5 1
Firefox 4.8 1
IE 1 1
Mac OS 9 IE 2.1 1
Netscape 4.7 - crashed.

Win2k Pro Firefox 16 1
IE 10 1

--
Rob
Aug 9 '05 #6

P: n/a
Re context switching. The short answer, as far as I have been able to
ascertain is NO, there is never context switching in execution of
setInterval and setTimeOut. The event engine (in the browser) is
single threaded. The interval in setTimeOut is more like a suggestion
than a fact. If the engine takes longer than the specified time to get
back to it's idle loop then that is when your callback will be
executed.
Bob

Aug 9 '05 #7

P: n/a
JRS: In article <42f7fc38$0$32245$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Tue, 9 Aug 2005 10:43:27, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted :
Dr John Stockton wrote:
[...]

Now
<URL:http://www.merlyn.demon.co.uk/js-dates.htm#Ress> or scan for RobG .

I'd be pleased to have interval and resolution data for other (common)
systems.


OK, here's some results. Mac OS 9 was done in classic mode under OS
X, I couldn't get results from Communicator (version 4.7) it just kept
crashing. I may update the version and try again later.

System Browser Update Resolution

Mac OS X Safari 1.5 1
Firefox 4.8 1
IE 1 1
Mac OS 9 IE 2.1 1
Netscape 4.7 - crashed.

Win2k Pro Firefox 16 1
IE 10 1


Thanks : I've included those in the Table.

My Win98 system should average interval 864E5/0x1800B0 = 54.9; I believe
that those showing 15 or 16 average 15.625 = 1/64, and that 10 is exact.
Other figures nave no obvious explanation.

I've also made provision for using other numbers of loops in the testing
- but IIRC the line containing N = +Num.value will need upgrading
for some systems : suggestions?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 9 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.