Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahirschberg/3a15c34aeff8278c7c5d to your computer and use it in GitHub Desktop.
Save ahirschberg/3a15c34aeff8278c7c5d to your computer and use it in GitHub Desktop.
Useful example of base variables referenced in a callback remaining in scope
// adapted from displayAppFiles() in app.js
function checkIfAnyFiles() {
var items_found = 0; // variable defined in outer function
var each_item = function(result) {
items_found++; // still in scope for the callback
}
var when_done = function () {
// Output depends on number of items traversed by each_item
console.log('found ' + items_found + ' files');
if (items_found == 0) {
// ... write 'no files found' to the listview
}
}
enumerateFiles({ // ... ,
func_each: each_item, // functions are only called inside #enumerateFiles
func_done: when_done
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment