Khởi tạo số UUID trong Javascript

JAVASCRIPT:

  1.  
  2. /* randomUUID.js - Version 1.0
  3. *
  4. * Copyright 2008, Robert Kieffer
  5. *
  6. * This software is made available under the terms of the Open Software License
  7. * v3.0 (available here: http://www.opensource.org/licenses/osl-3.0.php )
  8. *
  9. * The latest version of this file can be found at:
  10. * http://www.broofa.com/Tools/randomUUID.js
  11. *
  12. * For more information, or to comment on this, please go to:
  13. * http://www.broofa.com/blog/?p=151
  14. */
  15.  
  16. /**
  17. * Create and return a "version 4" RFC-4122 UUID string.
  18. */
  19. function randomUUID() {
  20.   var s = [], itoh = '0123456789ABCDEF';
  21.  
  22.   // Make array of random hex digits. The UUID only has 32 digits in it, but we
  23.   // allocate an extra items to make room for the '-'s we'll be inserting.
  24.   for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0×10);
  25.  
  26.   // Conform to RFC-4122, section 4.4
  27.   s[14] = 4// Set 4 high bits of time_high field to version
  28.   s[19] = (s[19] & 0×3) | 0×8;  // Specify 2 high bits of clock sequence
  29.  
  30.   // Convert to hex chars
  31.   for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
  32.  
  33.   // Insert '-'s
  34.   s[8] = s[13] = s[18] = s[23] = '-';
  35.  
  36.   return s.join('');
  37. }

minh.tran Javascript, Lập trình web http://feeds.feedburner.com/~r/ajaxian/~3/386692596/uuid-generator-in-javascript


Ðược xem 844 lần, 1 hôm nay

Bài viết liên quan

  1. Chưa có nhận xét.
  1. Chưa có trackbacks