以下の機能に問題があります。私の 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);
}