生成随机数 Jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19414506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate random numbers Jquery
提问by Modelesq
I'd like to show a random numbers within my list. Im grabbing the list from the backend, and im just using these numbers as examples. This is my code, I'm not quite sure why its not filling in the text.
我想在我的列表中显示一个随机数。我从后端获取列表,我只是使用这些数字作为示例。这是我的代码,我不太确定为什么不填写文本。
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('.num-gen').text(number);
},
Made a fiddle hereto show you what I'm working with. Thanks in advance.
回答by PhilTrep
You had an error in your code, you were missing the finishing , 1000/*time in ms*/);
!
你的代码有错误,你错过了整理, 1000/*time in ms*/);
!
Also, jQuery is not required here, you may do this in Javascript like this.
此外,这里不需要 jQuery,您可以像这样在 Javascript 中执行此操作。
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
document.getElementsByClass('num-gen')[0].innerHtml = number;
}, 1000);
EDIT:
编辑:
setInterval(function() {
jQuery.each(jQuery('.num-gen'),function(){
var number = 1 + Math.floor(Math.random() * 6);
jQuery(this).text(number);
});
}, 1000);
回答by I?ya Bursov
you have syntax error in you code, change it to:
您的代码中有语法错误,请将其更改为:
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('.num-gen').text(number);
}, 10);