Skip to content

Instantly share code, notes, and snippets.

@funkatron
Created December 9, 2010 04:04
Show Gist options
  • Save funkatron/734322 to your computer and use it in GitHub Desktop.
Save funkatron/734322 to your computer and use it in GitHub Desktop.
Recursion on an array of URLs in JavaScript until one succeeds or the end is reached
var urls = [
'http://getspaz.com/nosir!',
'http://funkatron.com/',
'http://getspaz.com/nosir!'
];
var index = 0;
function tryme() {
if (index < urls.length) {
$.ajax({
'url':urls[index],
'error':function() {
alert('failed! on '+urls[index]);
index++;
tryme();
},
'success':function() {
alert('succeeded! on '+urls[index]);
return;
}
});
} else {
alert('no more URLs to try!');
}
}
tryme();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment