Joel Dart wrote:
> You also probably have a helper method in your
> base library, for example in jQuery, jQuery.now()
> wraps (new Date()).getTime()

ECMA-262 5th edition defines `Date.now()` method which returns the
number of ms since Unix epoch (1 January 1970 00:00:00 UTC), and so is
equivalent to:

+new Date()
Number(new Date())
(new Date()).getTime()

Since not all browsers implement this feature you can define it
conditionally on the `Date` object:

if (!Date.now) {
  Date.now = function now() {
    return +new Date();
  };
}

Now you've got a cross-browser, readable timestamp without any library
and right on your native `Date` object. Also note, that augmenting
natives in this manner is not considered to be a bad pratice, since
you're emulating a feature that is existent in future browsers (and so
it helps your application intergrity).


Shane Tomlinson wrote:
> but [timestamp], unless the user resets their system
> clock is guaranteed to be unique.

Nothing is perfect however. And so much is the case with timestamps as
unique IDs.

| When using now to create timestamps or unique IDs,
| keep in mind that the resolution may be 15 milliseconds
| on Windows (see bug 363258 ), so you could end up with
| several equal values if now is called multiple times within
| a short time span. [1]

So if you generate timestamps on Windows and your the elapsed time is
less than 15ms you may end up using the same ID twice, which won't be
unique.

- Balázs


[1] 
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/now


2010/12/20 Joel Dart <[email protected]>:
> You also probably have a helper method in your base library, for example in 
> jQuery, jQuery.now() wraps (new Date()).getTime()
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On 
> Behalf Of Ben Barber
> Sent: Monday, December 20, 2010 12:23 PM
> To: [email protected]
> Subject: Re: [JSMentors] Image polling through Javascript
>
> I'm a fan of using implicit number conversion on Dates using the unary '+'
> operator (http://xkr.us/articles/javascript/unary-add/).
>
>    "http://....gif?"; + ( +new Date() );
>
>
> Ben Barber
>
>
> On Mon, 20 Dec 2010 04:44:44 -0600, Amit Agarwal <[email protected]>
> wrote:
>
>> yeah, I liked the timestamp concept.
>>
>> Modified my code and worked perfectly :)
>>
>> Thanks!
>>
>> -Regards
>> *Amit Agarwal <http:///www.amitagrwal.com>*
>> +91-779-822-8765
>>
>>
>>
>> On Mon, Dec 20, 2010 at 3:35 PM, Shane Tomlinson <[email protected]>
>> wrote:
>>
>>> I personally like to use timestamps over ints or rand, a timestamp is
>>> ensured to be unique.  If you use an int and in this session you had
>>> "http://.....gif?1";, the next time the user goes into the app, they
>>> re-use the integer 1, using the cache.  And Math.random isn't as
>>> random as you want it to be:
>>> http://stackoverflow.com/questions/1062902/how-random-is-javascripts-math-random.
>>>  But a timestamp, that is going to be unique unless you reset your
>>> system's clock.
>>>
>>> And performance wise, trying to optimize this is a
>>> premature-optimization.
>>>  Unless your request is called many many thousands of times a second,
>>> the performance overhead of using a timestamp will be effectively 0.
>>>
>>> Check out:
>>> https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/
>>> Date/getTime, this returns the number of milliseconds since Jan 1,
>>> 1970, again, unless the user resets their system clock, it is
>>> guaranteed to be unique.
>>>
>>> try requesting: "http://....gif?"; + ( new Date() ).getTime();
>>>
>>>
>>> On Mon, Dec 20, 2010 at 9:45 AM, Loic Giraudel
>>> <[email protected]>wrote:
>>>
>>>> Or for better performance a simple incremented int variable.
>>>>
>>>>
>>>> On Mon, Dec 20, 2010 at 10:43, fernando trasvina
>>>> <[email protected]>wrote:
>>>>
>>>>> use ?Math.random() on ur uri
>>>>> or timestamp + Math.random()
>>>>>
>>>>> On Dec 20, 2010, at 3:35 AM, Amit Agarwal wrote:
>>>>>
>>>>> Hi every one,
>>>>>
>>>>> I need to poll an image using javascript and need to perform an
>>>>> action once the image is found at its position. This is the code I
>>>>> have written for this task.
>>>>>
>>>>> /*----Image handling script starts here----*/ var beacon = new
>>>>> Image();
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> beacon.onload = function() {
>>>>>
>>>>>
>>>>>     console.log('Image found');
>>>>>     console.log(this.width,this.height);
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>     window.clearInterval(timer);
>>>>> };
>>>>>
>>>>>
>>>>> beacon.onerror = function(){
>>>>>    console.log('Image not found');
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> }
>>>>> var timer = window.setInterval(function(){
>>>>>
>>>>>
>>>>>     console.log('sending the request again');
>>>>>     beacon.src =
>>>>> "http://www.google.co.in/logos/2010/lennon10-hp.gif";;
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> },2000);
>>>>> /*----Image handling script ends here----*/
>>>>>
>>>>> Problem is that, after one GET request, the response gets cached
>>>>> and requests don't get sent everytime I set src. If you examine NET
>>>>> tab, it sends request only on first src set and caches the
>>>>> response.
>>>>>
>>>>> I need to send a fresh request for image every time my code sets
>>>>> the src.
>>>>> Any workarounds?
>>>>>
>>>>> -Regards
>>>>> *Amit Agarwal <http:/www.amitagrwal.com>*
>>>>> +91-779-822-8765
>>>>>
>>>>>
>>>>> --
>>>>> To view archived discussions from the original JSMentors Mailman list:
>>>>> http://www.mail-archive.com/[email protected]/
>>>>>
>>>>> To search via a non-Google archive, visit here:
>>>>> http://www.mail-archive.com/[email protected]/
>>>>>
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]
>>>>>
>>>>>
>>>>>  --
>>>>> To view archived discussions from the original JSMentors Mailman list:
>>>>> http://www.mail-archive.com/[email protected]/
>>>>>
>>>>> To search via a non-Google archive, visit here:
>>>>> http://www.mail-archive.com/[email protected]/
>>>>>
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]<jsmentors%2bunsubscr...@goog
>>>>> jsmentors+legroups.com>
>>>>>
>>>>
>>>>  --
>>>> To view archived discussions from the original JSMentors Mailman list:
>>>> http://www.mail-archive.com/[email protected]/
>>>>
>>>> To search via a non-Google archive, visit here:
>>>> http://www.mail-archive.com/[email protected]/
>>>>
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<jsmentors%2bunsubscr...@googl
>>>> jsmentors+egroups.com>
>>>>
>>>
>>>  --
>>> To view archived discussions from the original JSMentors Mailman list:
>>> http://www.mail-archive.com/[email protected]/
>>>
>>> To search via a non-Google archive, visit here:
>>> http://www.mail-archive.com/[email protected]/
>>>
>>> To unsubscribe from this group, send email to
>>> [email protected]<jsmentors%2bunsubscr...@google
>>> jsmentors+groups.com>
>>>
>
> --
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
> --
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to