I am creating some code that downloads a file from a website and unzips it automatically. I have made it so that it will check that the latest version of the file has been downloaded every 4 hours, and if it already has been downloaded, then it won't download it again. Now I want to make it so that if the download fails for some reason, rather than waiting another 4 hours to download the file again, it will attempt to download it again instantly, and will keep trying to do that until it downloads successfully.
I have used the "try" and "catch" statements to make it so that if it will try and download the file, and if the file download fails, it will catch the error, and then in the catch statement it will do the same thing but do it every 5 seconds instead of every 4 hours.
try{
interval()
//this is referencing a function that downloads the file every 4 hours
setTimeout(stopInterval, 5000);
//this is to prevent the function from stacking every time the try statement is called
}
}
catch(e){
checkInterval()
//this is referencing a function that does the same as what's in the try statement but does it every 5 seconds instead
}
}
what it successfully does is that if it detects an error within the code itself (e.g. the function it's trying to call is misspelt), it will do what I am asking it to do which is repeat the function every 5 seconds. But what I want it to do is exactly that, except that it will apply to other errors like the internet disconnecting. I have tried using the "Throw New Error" construct but don't really know how to use it properly, or how to specifically apply it to errors related to internet or other problems. If there is a way to do this with the try catch statement, that would be awesome, but if I have to do something else then any help would be appreciated. I am new to this site so sorry if this isn't explained very well, if there is anything you need me to clarify feel free to let me know. Thanks in advance!