Basically just looking to see if a particular txt file exists on the server, and if so, do further processing; however, I don’t think my recursion is correct, so can someone offer a few pointers – here’s what I have:
function fileExists(filename) {
$.ajax({
type: 'HEAD',
url: 'http://www.example.com/system/'+filename+'.txt',
success: function() {
// Further processing if file exists
},
error: function() {
// File does not exists, run through function again-
return arguments.callee(filename);
}
});
}
It’s pretty basic, there’s some processing before hand that actually creates the file; however the issue is it’s FTP-ed up to our domain, which means timing can vary by a few seconds, so basically I just want it to recheck until it sees that the file exists. I’ll modify it a little afterwards to control the stack, possibly setting a timeout of half a second or something, but I’m not that great with javascript, so I need a few pointers to make this recursive. Any help is GREATLY appreciated.
the issue is when you try to call
fileExistsagain viaarguments.callee(fileName), the scope of theerrormethod isn’t what you think it is.Just call
fileExists.The other you are going to have is that if your server is quick, you are going to be firing a ton of requests. You probably want to wait some time between requests. So make
errorcontainFinally, you should realize that the
errorcallback only gets invoked if the server returns a 500. The 500 code usually means there was an error on your server. If a file doesn’t exist, you should probably return json to indicate the file doesn’t exist, and handle that case in yoursuccesscallback.