JavaScript jQuery .now() not unique jquery unique array,jquery unique id,jquery uniquesort,jquery unique

You could have a counter which increments for each object. You could then add this counter to $.now() to make sure it's unique:

obj = {};
obj[0] = 'hej';
obj[1] = 'hopp';
counter=1;

$.each( obj, function( key, value ) {
    console.log($.now() + counter);
    counter+=1;
});
Answer:1

jQuery.now() isn't supposed to give you a unique number, just a " a number representing the current time" (http://api.jquery.com/jQuery.now/). More specifically "the number of milliseconds since 1 January 1970 00:00:00 UTC" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime).

The simplest way to generate a unique identifier depends on the scope the identifier has to be unique in.

1: If the identifier just has to be unique within the scope of the function, a counter as suggested by SteveP would probably do the job best.

2: If the identifier needs to be unique within the scope of your application - and you do not have the possibility to keep track of the assigned identifiers (as in 1) - your job is more difficult. In real-live applications such a scenario could be tackled by combining the timestamp with a random number, along the lines of

var identifier = $.now() + "_" + Math.round(Math.random() * 1e10)

This approach doesn't guarantee for true uniqueness of identifier though since Math.round(Math.random() * 1e10) could return the same value twice in one millisecond. Depending on how often you generate a string like this, it will be still pretty unlikely you end up with the same string twice.

3: If you however aim for "uniqueness across space and time", you could get inspiration from http://www.ietf.org/rfc/rfc4122.txt

It should be noted that pure Javascript implementations like Create GUID / UUID in JavaScript? only work with random numbers as well and will thus not generate truly unique identifiers. For a truly unique identifier across space and time a truly unique element such as a mac-address has to be used as part of the identifier.

Answer:2

Quote from jquery api:

The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().

So you basically use new Date().getTime(). However getTime() isn't accurate... Interresting quote from this article:

Effectively these browsers are only updating their internal getTime representations every 15 milliseconds. This means that if you attempt to query for an updated time it’ll always be rounded down to the last time the timer was updated (which, on average, will have been about 7.5 milliseconds ago).

In short: getTime() isn't that accurate (in some browsers).

When you need a random number (as you do), use:

var randomVal = Math.random(); // e.g. 0.6137274974025786
var randomValI = Math.round(Math.random()*100000000000000000)  // e.g. 6137274974025786

When you need a more formal 'guid', use something like this:

function createGUID() {
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 
    }

    // then to call it, plus stitch in '4' in the third group
    return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
}
var guid = createGUID();
Answer:3

I am trying to animate open (+) and close (x) buttons for main navigation. It's not working properly and I am getting this error. Uncaught TypeError: Cannot read property 'addEventListener' of null ...

I am trying to animate open (+) and close (x) buttons for main navigation. It's not working properly and I am getting this error. Uncaught TypeError: Cannot read property 'addEventListener' of null ...

  1. cannot read property of undefined
  2. cannot read property 'length' of undefined
  3. cannot read property 'setstate' of undefined
  4. cannot read property 'push' of undefined
  5. cannot read property 'map' of undefined
  6. cannot read property 'addeventlistener' of null
  7. cannot read property 'match' of undefined
  8. cannot read property 'then' of undefined
  9. cannot read property '0' of undefined
  10. cannot read property 'props' of undefined
  11. cannot read property 'nativeelement' of undefined
  12. cannot read property 'tolowercase' of undefined
  13. cannot read property 'state' of undefined
  14. cannot read property 'foreach' of undefined
  15. cannot read property 'split' of undefined
  16. cannot read property of null
  17. cannot read property 'replace' of undefined
  18. cannot read property 'summary' of undefined
  19. cannot read property 'createelement' of undefined
  20. cannot read property 'subscribe' of undefined

I am buried in a RegExp hell and can't find way out, please help me. I need RegExp that matches only numbers (at least 1 number) and one of this characters: <, >, = (exactly one of them one time). ...

I am buried in a RegExp hell and can't find way out, please help me. I need RegExp that matches only numbers (at least 1 number) and one of this characters: <, >, = (exactly one of them one time). ...

  1. regex numbers special characters
  2. regex letters numbers special characters
  3. regex numbers and special characters only
  4. regex no numbers or special characters
  5. regex remove numbers and special characters
  6. regex match numbers and special characters
  7. regex for numbers and special characters javascript
  8. regex exclude numbers and special characters
  9. regex for numbers and special characters in java
  10. c# regex numbers and special characters
  11. regex only numbers and special characters javascript
  12. regex remove numbers and special characters c#
  13. regex remove numbers and special characters javascript
  14. regex only numbers without special characters
  15. regex special numbers

I'm writing a simple text-base game in my free time. It's called TARDIS flight (Doctor Who!) and I'm working on the main menu. So I'm using a function, addMainMenuListeners, to add all the event ...

I'm writing a simple text-base game in my free time. It's called TARDIS flight (Doctor Who!) and I'm working on the main menu. So I'm using a function, addMainMenuListeners, to add all the event ...

I need your help, if the following code below counts every table row in a table, how can it be amended such that the code won't count the <th></th> as a row itself? var rows = document....

I need your help, if the following code below counts every table row in a table, how can it be amended such that the code won't count the <th></th> as a row itself? var rows = document....

  1. counting number rows sql
  2. counting rows number
  3. counting number of rows in excel
  4. counting number of rows in r
  5. counting number of rows in python
  6. counting number of rows in pandas
  7. counting number of rows in dataframe
  8. counting number of rows in pandas dataframe
  9. counting number of rows in excel vba
  10. counting number of rows in vba
  11. counting number of rows in mysql
  12. counting number of rows in csv python
  13. counting number of rows in sas
  14. counting number of rows in php
  15. counting number of rows in excel after filter
  16. counting number of rows in a range vba
  17. counting number of rows in excel using vba
  18. counting number of rows in dataframe r
  19. counting number of rows in hive
  20. counting number of rows in a table