Javascript 中的真正随机字符串异步
我正在使用 javascript 函数生成随机字符串:
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
/*$.ajax({
type: "GET",
url: "uuid.php",
cache: false,
success: function(html){
return html;
}
});*/
return (S4()+S4()+S4()+S4());
}
我想让它利用我找到的 php uuid 库,问题是我需要它在 javascript 中运行。我经常使用 guid() 函数,并且我一直在尝试想出一种优雅的方式来获取 uuid,我请求使用 ajax 对象(上面已注释掉)。每次只打印随机 uuid 的 uuid 页面就位于该页面的本地旁边。我不想使请求同步,因为就像我说的,我经常使用它,并且不希望每次这个东西发出请求时一切都停止。或者也许有一种我可以使用 jQuery 的方法,它既快又不影响性能?
我并不反对稍微改变一下,就像这里的最佳实践是在加载时获取 uuid 吗?但我生成的 UUID 数量是完全动态的,并且取决于用户。
谢谢你!
I am using a javascript function to generate a random string:
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
/*$.ajax({
type: "GET",
url: "uuid.php",
cache: false,
success: function(html){
return html;
}
});*/
return (S4()+S4()+S4()+S4());
}
And I want to make it utilize a php uuid library that I've found, the problem is I need it to run in javascript. I use the guid() function a lot and I've been trying to think of an elegant way of grabbing the uuid, that I request using the ajax object (commented out above). The uuid page that just prints random uuids each time is sitting locally next to this page. I would not like to make the request synchronous because, like I said, I use it quite a bit and would prefer to not have everything halt every time this thing makes a request. Or perhaps there's a method I could use of jQuery that be as fast and not hinder performance?
I'm not adverse to changing things up a bit, like would the best practice here to acquire a uuid on load? But the number of UUIDs I generate is completely dynamic, and dependent upon the user.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查
uniqid()
函数来自 phpjs.org。Check the
uniqid()
function from phpjs.org.如何向 guid() 函数添加一个回调参数,您可以在其中为某些内容赋值:
How about adding a callback argument to the guid() function, wherein you can assign a value to something: