1

我对下面列出的功能有疑问。在我的 PC 上,这段代码运行良好,在我的手机上的 Chrome 上也是如此。但是,当我在默认的 Android 浏览器中运行此代码时,结果是排序数组的前 4 个元素不是它们应该的;

  • 元素 1:0
  • 元素 2:128
  • 元素 3:256
  • 元素 4:384

现在我想知道为什么会这样,直到对数组进行排序,收集的值都很好,之后,它们不正确......

根据此链接,它应该与默认的 Android 浏览器兼容: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Browser_compatibility

function sendWeekProgram() {
//Get all the info to send
var weekProgramData;
//Check if the week program is enabled
if($("#vacationMode").prop('checked') === false) {
    weekProgramData = '<week_program state="on">';
}else {
    weekProgramData = '<week_program state="off">';
}

//For each day get the switches
for(i = 0; i < 7; i++) {
    //Set the day name
    switch(i) {
        case 0:
            dayName = "Monday";
            break;
        case 1:
            dayName = "Tuesday";
            break;
        case 2:
            dayName = "Wednesday";
            break;
        case 3:
            dayName = "Thursday";
            break;
        case 4:
            dayName = "Friday";
            break;
        case 5:
            dayName = "Saturday";
            break;
        case 6:
            dayName = "Sunday";
            break;
    }

    //Add the day to the query
    weekProgramData = weekProgramData + '<day name="' + dayName + '">';

    //Create an array
    var arraySwitches = [];

    //Put all day switches in an array
    for(j = 0; j < 5; j++) {
        //Check if the switch is on or off
        if(($("#daySwitch" + dayName + (j + 1)).hasClass("dayOverViewTextActive") === false) || ($("#nightSwitch" + dayName + (j + 1)).hasClass("dayOverViewTextActive") === false)) {
            arraySwitches[j] = {type: 'day', state: 'off', value: parseInt($("#daySwitch" + dayName + (j + 1)).text().replace(":", ""))};
        }else {
            arraySwitches[j] = {type: 'day', state: 'on', value: parseInt($("#daySwitch" + dayName + (j + 1)).text().replace(":", ""))};
        }
    }

    //Put all night switches in an array
    for(j = 5; j < 10; j++) {
        //Check if the switch is on or off
        if(($("#daySwitch" + dayName + (j-4)).hasClass("dayOverViewTextActive") === false) || ($("#nightSwitch" + dayName + (j-4)).hasClass("dayOverViewTextActive") === false)) {
            arraySwitches[j] = {type: 'night', state: 'off', value: parseInt($("#nightSwitch" + dayName + (j-4)).text().replace(":", ""))};
        }else {
            arraySwitches[j] = {type: 'night', state: 'on', value: parseInt($("#nightSwitch" + dayName + (j-4)).text().replace(":", ""))};
        }
    }

    //Sort the array
    arraySwitches.sort(function(a, b) {
        return a.value
             - b.value;
    });

    //For each element in the array, add it to the weekProgramData
    for(k = 0; k < 10; k++) {
        //Add leading zeroes
        arrayValue = arraySwitches[k].value.toString();
        while(arrayValue.length < 4) {
            arrayValue = "0" + arrayValue;
        }

        //First convert the integer back to the time
        time = arrayValue.substr(0,2) + ':' + arrayValue.substr(2);

        //Add value to weekprogram
        weekProgramData = weekProgramData + '<switch type="' + arraySwitches[k].type + '" state="' + arraySwitches[k].state + '">' + time + '</switch>';
    }

    //Add the closing tag of the day
    weekProgramData = weekProgramData + '</day>';
}

//Add the closing tag of the week program
weekProgramData = weekProgramData + '</week_program>';

//Send the info
putInfo("weekProgram", weekProgramData);
}
4

0 回答 0