[英]Why i sometimes got error and sometimes not?
This code is supposed to get random JSON data from an array when the user clicks the button.当用户单击按钮时,此代码应该从数组中获取随机 JSON 数据。 I'm using a function to return a random number.我正在使用 function 返回一个随机数。 But when I load the page and click on the button, sometimes I get random data, while sometimes I get an error.但是当我加载页面并单击按钮时,有时我会得到随机数据,而有时我会出错。
Why is this?为什么是这样? Thanks in advance.提前致谢。
$(document).ready(function () {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function renderQuiz() {
console.log(userdata.USER.TOPSONGS[(getRandomInt(0,2))][(getRandomInt(0,4))].snippet);
}
...
$('#button').click(function () {
renderQuiz();
console.log("userprogress: " + userprogress);
if (userprogress < 10) {
userprogress++;
$('#answered').text(`${userprogress} / 10`);
}
else { window.location.href = '/result'; }
return;
});
}); //end document.ready
Does userdata.USER.TOPSONGS
contain a 3x5 array in which all values are defined and all values have a .snippet
? userdata.USER.TOPSONGS
是否包含一个 3x5 数组,其中定义了所有值并且所有值都有一个.snippet
?
Mostly probably, userdata.USER.TOPSONGS
does not contain [x][y].snippet
for some values 0 <= x <= 2
and 0 <= y <= 4
, causing this error.很可能,对于某些值0 <= x <= 2
和0 <= y <= 4
, userdata.USER.TOPSONGS
不包含[x][y].snippet
,从而导致此错误。
Please check your userdata.USER.TOPSONGS
array and/or adjust the minimum and maximum values being passed to the random number function.请检查您的userdata.USER.TOPSONGS
数组和/或调整传递给随机数 function 的最小值和最大值。
Looks like reaching out of the boundaries of the array.看起来像是超出了数组的边界。 Your getRandomInt(min, max)
returns random int including min
and max
values.您的getRandomInt(min, max)
返回随机 int,包括min
和max
。 Check if this is okay.检查这是否正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.