生成唯一ID的最佳方法客户端(使用Javascript) [英] Best way to generate unique ids client-side (with Javascript)

查看:109
本文介绍了生成唯一ID的最佳方法客户端(使用Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在浏览器中生成唯一的ID。目前,我正在使用它:

I need to generate unique ids in the browser. Currently, I'm using this:

Math.floor(Math.random() * 10000000000000001)

我想使用当前的UNIX时间((new Date).getTime()),但我担心如果两个客户在完全同时生成id,它们将不是唯一的。

I'd like to use the current UNIX time ((new Date).getTime()), but I'm worried that if two clients generate ids at the exact same time, they wouldn't be unique.

我可以使用当前的UNIX时间(我想这样,因为这种方式会存储更多信息)?如果没有,最好的方法是什么(可能是UNIX时间+ 2个随机数字?)

Can I use the current UNIX time (I'd like to because that way ids would store more information)? If not, what's the best way to do this (maybe UNIX time + 2 random digits?)

推荐答案

你可以创建一个GUID使用以下链接:

you can create a GUID using the following links:

http://softwareas.com/guid0-a-javascript-guid-generator

在JavaScript中创建GUID / UUID?

这将最大化你的唯一性的可能性。

This will maximise your chance of "uniqueness."

或者,如果它是一个安全页面,您可以将日期/时间与用户名连接起来,以防止同时生成多个值。

Alternatively, if it is a secure page, you can concatenate the date/time with the username to prevent multiple simultaneous generated values.

这篇关于生成唯一ID的最佳方法客户端(使用Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆