Skip to main content

How to emulate Event.timeStamp


Event.timeStamp




The timeStamp attribute must return the value it was initialized to. When an event is created the attribute must be initialized to the number of milliseconds that has passed since 00:00:00 UTC on 1 January 1970.




One could trap both new Event and document.createEvent to set the timeStamp accordingly but how do you intercept events created and dispatched by the browser?



One could add an event listener (capture phase) to the document that listens on "every" event type and write the timeStamp as close to the dispatch time but that would be an ugly hack.



  • Are there any better ways to emulate Event.timeStamp ?

  • Are there any potential traps with intercepting new Event / new CustomEvent and document.createEvent .

  • Are there other ways to create events programmaticly ?

  • Are there any potential issues with adding event listeners to document and manually setting timeStamp as early as possible ?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I couldn't find any place to intercept the creation of events that were generated by the browser rather than by user code. Your "ugly hack" seems to work okay though:

    addEventListener("click", function (e) {
    Object.defineProperty(e, "timeStamp", {
    get: function () { return 4; }
    });
    }, true);


    Obviously you'd have to call addEventListener a bunch of times with whatever event names you're interested in. Note that setting the timeStamp directly has no effect, but defineProperty works. I only tested Chrome and IE9; I'm sure interop would be a mess since we're using a getter method.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.