@@ -155,7 +155,7 @@ function cacheSettings()
155
155
setCache ( `pia_set_${ set . Code_Name } ` , set . Value )
156
156
setCache ( `pia_set_opt_${ set . Code_Name } ` , resolvedOptions )
157
157
} ) ;
158
- } ) . then ( ( ) => handleSuccess ( 'cacheSettings' ) ) . catch ( ( ) => handleFailure ( 'cacheSettings' ) ) ; // handle AJAX synchronization
158
+ } ) . then ( ( ) => handleSuccess ( 'cacheSettings' ) ) . catch ( ( ) => handleFailure ( 'cacheSettings' , cacheSettings ) ) ; // handle AJAX synchronization
159
159
} )
160
160
}
161
161
@@ -221,7 +221,7 @@ function cacheStrings()
221
221
data . forEach ( ( langString ) => {
222
222
setCache ( `pia_lang_${ langString . String_Key } _${ langString . Language_Code } ` , langString . String_Value )
223
223
} ) ;
224
- } ) . then ( ( ) => handleSuccess ( 'cacheStrings' ) ) . catch ( ( ) => handleFailure ( 'cacheStrings' ) ) ; // handle AJAX synchronization
224
+ } ) . then ( ( ) => handleSuccess ( 'cacheStrings' ) ) . catch ( ( ) => handleFailure ( 'cacheStrings' , cacheStrings ) ) ; // handle AJAX synchronization
225
225
226
226
}
227
227
@@ -891,7 +891,7 @@ function initDeviceListAll_JSON()
891
891
setCache ( 'devicesListAll_JSON' , devicesListAll_JSON_str )
892
892
893
893
// console.log(getCache('devicesListAll_JSON'))
894
- } ) . then ( ( ) => handleSuccess ( 'initDeviceListAll_JSON' ) ) . catch ( ( ) => handleFailure ( 'initDeviceListAll_JSON' ) ) ; // handle AJAX synchronization
894
+ } ) . then ( ( ) => handleSuccess ( 'initDeviceListAll_JSON' ) ) . catch ( ( ) => handleFailure ( 'initDeviceListAll_JSON' , initDeviceListAll_JSON ) ) ; // handle AJAX synchronization
895
895
896
896
}
897
897
@@ -1074,6 +1074,13 @@ function resolveParams(params, template) {
1074
1074
return template ;
1075
1075
}
1076
1076
1077
+ // -----------------------------------------------------------------------------
1078
+ // check if two arrays contain same values even if out of order
1079
+ function arraysContainSameValues ( arr1 , arr2 ) {
1080
+ // Sort and stringify arrays, then compare
1081
+ return JSON . stringify ( arr1 . slice ( ) . sort ( ) ) === JSON . stringify ( arr2 . slice ( ) . sort ( ) ) ;
1082
+ }
1083
+
1077
1084
// -----------------------------------------------------------------------------
1078
1085
// initialize
1079
1086
// -----------------------------------------------------------------------------
@@ -1147,8 +1154,9 @@ function executeOnce() {
1147
1154
1148
1155
showSpinner ( )
1149
1156
1150
- // Counter to keep track of completed AJAX calls
1151
- completedCalls = 0 ;
1157
+ // to keep track of completed AJAX calls
1158
+ completedCalls = [ ]
1159
+ completedCalls_final = [ 'cacheSettings' , 'cacheStrings' , 'initDeviceListAll_JSON' ] ;
1152
1160
1153
1161
// Your initialization code here
1154
1162
cacheSettings ( ) ;
@@ -1161,22 +1169,26 @@ function executeOnce() {
1161
1169
// Function to handle successful completion of an AJAX call
1162
1170
const handleSuccess = ( callName ) => {
1163
1171
console . log ( `AJAX call ${ callName } successful` ) ;
1164
- completedCalls ++ ;
1172
+ // store completed call
1173
+ completedCalls . push ( callName )
1165
1174
onAllCallsComplete ( ) ;
1166
1175
} ;
1167
1176
1168
1177
// -----------------------------------------------------------------------------
1169
1178
// Function to handle failure of an AJAX call
1170
- const handleFailure = ( callName ) => {
1179
+ const handleFailure = ( callName , callback ) => {
1171
1180
// Handle AJAX call failure here
1172
1181
console . error ( `AJAX call ${ callName } failed` ) ;
1182
+
1183
+ // try until successful
1184
+ callback ( )
1173
1185
} ;
1174
1186
1175
1187
// -----------------------------------------------------------------------------
1176
1188
// Function to execute when all AJAX calls have completed
1177
1189
const onAllCallsComplete = ( ) => {
1178
1190
// Check if all three AJAX calls have completed
1179
- if ( completedCalls === 3 ) {
1191
+ if ( arraysContainSameValues ( completedCalls , completedCalls_final ) ) {
1180
1192
1181
1193
// Set the flag in sessionStorage to indicate that the code has been executed
1182
1194
// and save time when last time the page was initialized
0 commit comments