用jQuery随机化数字吗? [英] Randomize numbers with jQuery?

查看:100
本文介绍了用jQuery随机化数字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一种简单的jQuery方法来随机创建数字,然后显示几秒钟后选择的数字1 -6? [像骰子]

Is there a simple jQuery way to create numbers randomly showing then a number 1 -6 is choosing after a few seconds? [Like dice]

推荐答案

这不需要jQuery。 JavaScript Math.random 函数返回0到1之间的随机数,因此,如果您想要1到6之间的数字,则可以执行以下操作:

This doesn't require jQuery. The JavaScript Math.random function returns a random number between 0 and 1, so if you want a number between 1 and 6, you can do:

var number = 1 + Math.floor(Math.random() * 6);

更新:(根据评论)如果要显示随机经常更改的数字,您可以使用 setInterval 创建计时器:

Update: (as per comment) If you want to display a random number that changes every so often, you can use setInterval to create a timer:

setInterval(function() {
  var number = 1 + Math.floor(Math.random() * 6);
  $('#my_div').text(number);
},
1000); // every 1 second

这篇关于用jQuery随机化数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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