I am having a weird issue with $timeout in AngularJS that only occurs in Safari (both iOS and macOS), but not in other browsers. Instead of waiting the specified milliseconds, Safari executes the function immediately. The entire code is available here, but this is the relevant part of the code:
app.directive('upload', ['$lhttp', '$http', '$interval', '$timeout', '$q', '$location', function ($lhttp, $http, $interval, $timeout, $q, $location) {
return {
templateUrl: '/.src/views/upload-widget.html',
controller: function($scope, $element, $attrs) {
// code ...
// Define upload function
vm.uploadFiles = ()=>{
// code ...
// Create the payload for the upload request
let formdata = new FormData();
formdata.append("salt", salt);
// code ...
// Create the upload request
let request = $http({
method: 'POST',
url: "/.src/php/upload.php",
data: formdata,
transformRequest: angular.identity,
uploadEventHandlers: {
progress: function(e) {
vm.progress = Math.round((e.loaded/e.total)*100) + "%";
if (vm.progress === '100%') vm.get_server_progress(salt);
}
},
headers: {'Content-Type': undefined}
});
// Create handler functions for success and errors
request.then(function successCallback (response) {
vm.server_progress = "100%";
vm.actually_loading = false;
$timeout(()=>{ vm.loading = false }, 600);
if (response.data.error.length) {
vm.fail = true;
vm.error_message = response.data.error;
}
if (!response.data.log.length) {
vm.fail = true;
vm.error_message = $scope.m.text.upload_error;
}
}, function errorCallback (response) {
// code ...
});
}
// code ...
// Create function for checking status of server side processing of the upload
vm.get_server_progress = (salt)=>{
$http.get('/.src/php/status.php?salt='+salt+'&d='+Date.now()).success((data)=>{
vm.server_progress = data+"%";
re_get_progress_if(salt);
}).error(()=>{
re_get_progress_if(salt);
});
}
function re_get_progress_if (salt) {
//////////////////////////////////////
// THIS IS THE PROBLEMATIC $TIMEOUT://
if (vm.actually_loading) $timeout(vm.get_server_progress, 100, true, salt);
}
},
controllerAs: 'up'
};
}]);
Does anyone know if this is a bug in Safari? If it is, how can I do a workaround?