Example 1-8. The jQuery.ready function can be synchronous or asynchronous

This example has been corrected from the original version by changing jQuery.ready(...) to jQuery(document).ready(...)


jQuery(document).ready(function () {
	// jQuery calls this function after the DOM is loaded and ready to use
	console.log('DOM is ready');
	setTimeout(foo, 0);
});

function foo() {
	jQuery(document).ready(function () {
		console.log('Ready listener inside foo');
	});

	console.log('Last line of the foo function');
}

console.log('I am the last line of the script');

// Console output
// I am the last line of the script
// DOM is ready
// Ready listener inside foo
// Last line of the foo function