Previous Page
Next Page

Recipe 4.11. Generating a Unique Number

Problem

You want to generate a unique number, such as a number to append to a URL to prevent caching of the URL.

Solution

Use the NumberUtilities.getUnique( ) method.

Discussion

Unique numbers are most commonly used to generate a unique URL (to prevent it from being cached). That is, by appending a unique number to the end of a URL, it is unlike any previous URL; therefore, the browser obtains the data from the remote server instead of the local cache.

The NumberUtilities.getUnique( ) method returns a number based on the current epoch milliseconds. (The following example assumes you've imported ascb.util.NumberUtilities.)

// Display a unique number.
trace(NumberUtilities.getUnique(  ));

In most circumstances the preceding code returns the current epoch in milliseconds. However, it is possible that you may want to generate a set of unique numbers in less than a millisecond of processing time. In that case, you'll find that the getUnique( ) method adds a random number to the epoch milliseconds to ensure a unique number. The following example generates more than one number within the same millisecond:

for(var i:Number = 0; i < 100; i++) {
  trace(NumberUtilities.getUnique(  ));
}


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)