Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Forked from rborn/app.js
Last active December 21, 2015 03:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronksaunders/6245483 to your computer and use it in GitHub Desktop.
Save aaronksaunders/6245483 to your computer and use it in GitHub Desktop.
var get_file = function(url, callback) {
var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
// this function is called when data is returned from the server and available for use
// this.responseText holds the raw text return of the message (used for text/JSON)
// this.responseXML holds any returned XML (including SOAP)
// this.responseData holds any returned binary data
console.log('downloaded '+ url);
callback({
url: url,
data: this.responseData
});
},
onerror: function(e) {
// this function is called when an error occurs, including a timeout
console.log('error '+ url);
callback({
url: url,
error: e.error
});
},
timeout: 5000
/* in milliseconds */
});
xhr.open("GET", url);
xhr.send(); // request is actually sent with this statement
};
var files_arr = [
'http://farm6.staticflickr.com/5132/5502816868_67309ca2e6_o.jpg',
'http://farm5.staticflickr.com/4062/4398223914_8039585c48_o.jpg',
'http://farm6.staticflickr.com/5094/5416175148_3753a5cc90_o.jpg',
'http://farm9.staticflickr.com/8044/8124564455_66bf80d3b7_o.jpg',
'http://farm9.staticflickr.com/8301/7863005744_942b6c64d7_o.jpg',
'http://farm8.staticflickr.com/7300/9194232482_1cee5642e2_o.jpg'
];
var current_download = 0;
var callback = function(){
current_download++;
Ti.API.info( 'Downloading '+current_download+' of '+ files_arr.length);
if ( current_download < files_arr.length) {
get_file(files_arr[current_download], callback);
}
};
get_file(files_arr[current_download], callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment