简体   繁体   English

为什么我有时会出错,有时不会?

[英]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 <= 20 <= y <= 4userdata.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,包括minmax Check if this is okay.检查这是否正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么静态var语法有时会抛出错误而有时却不会 - Why does static var syntax sometimes throw an error and sometimes not 为什么我有时需要使用JSON.stringify,有时候不需要 - Why do I need to use sometimes JSON.stringify and sometimes not 为什么 JavaScript 显示预期“;” 有时会出错? - Why JavaScript show expected “;” error sometimes? 为什么有时浏览器阻止弹出窗口,有时不会? - Why sometimes browser block popup and sometimes not? 为什么Firebug调试有时会起作用而有时却不起作用? - why does firebug debugging sometimes work and sometimes not? 为什么有时数据集未定义? - Why is dataset undefined sometimes? $ http有时返回错误代码0,有时可以工作 - $http returns error code 0 sometimes and works sometimes 为什么 Jest 有时会打印带有行指针的“漂亮”错误消息,有时不会? - Why does Jest sometimes prints "pretty" error messages with line pointers, and sometimes not? 为什么有时将_id作为字符串,有时将_id作为ObjectID? - Why do I sometimes get _id as a string and sometimes get _id as ObjectID? 我不知道为什么我的 get 请求有时有效但有时无效(404 未找到) - I don't know why my get request sometimes works but sometimes doesn't (404 not found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM