javascript timers doubt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vanisathish@gmail.com

    javascript timers doubt

    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

  • Roman Gordin
    New Member
    • Aug 2005
    • 4

    #2
    1. Use DateObject.getT ime() and .getMillisecond s() 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.

    Comment

    • RobG

      #3
      Re: javascript timers doubt

      vanisathish@gma il.com wrote:[color=blue]
      > 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[/color]

      Have a browse of the stuff here:

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

      and here:

      <URL:http://www.merlyn.demo n.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.
      [color=blue]
      >
      > 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..[/color]

      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

      Comment

      • vanisathish@gmail.com

        #4
        Re: javascript timers doubt

        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.?

        Comment

        • Dr John Stockton

          #5
          Re: javascript timers doubt

          JRS: In article <42f71210$0$164 86$5a62ac22@per-qv1-newsreader-
          01.iinet.net.au >, dated Mon, 8 Aug 2005 18:04:31, seen in
          news:comp.lang. javascript, RobG <rgqld@iinet.ne t.auau> posted :[color=blue]
          >
          > <URL:http://www.merlyn.demo n.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.[/color]

          Now
          <URL:http://www.merlyn.demo n.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • RobG

            #6
            Re: javascript timers doubt

            Dr John Stockton wrote:
            [...][color=blue]
            >
            > Now
            > <URL:http://www.merlyn.demo n.co.uk/js-dates.htm#Ress> or scan for RobG .
            >
            > I'd be pleased to have interval and resolution data for other (common)
            > systems.
            >[/color]

            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

            Comment

            • bgulian@gmail.com

              #7
              Re: javascript timers doubt

              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

              Comment

              • Dr John Stockton

                #8
                Re: javascript timers doubt

                JRS: In article <42f7fc38$0$322 45$5a62ac22@per-qv1-newsreader-
                01.iinet.net.au >, dated Tue, 9 Aug 2005 10:43:27, seen in
                news:comp.lang. javascript, RobG <rgqld@iinet.ne t.auau> posted :[color=blue]
                >Dr John Stockton wrote:
                >[...][color=green]
                >>
                >> Now
                >> <URL:http://www.merlyn.demo n.co.uk/js-dates.htm#Ress> or scan for RobG .
                >>
                >> I'd be pleased to have interval and resolution data for other (common)
                >> systems.
                >>[/color]
                >
                >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[/color]

                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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                Working...