Delaying until condition is fulfilled

In javascript, we can use setTimeout to do call itself recursively until a condition is met.

Here, we want to check the html content with an interval of 300ms until it is populated, then we will do something.

function checkPostCode() {
	
	if ($('#my_postcode').html() == '') {
		setTimeout(function() {
			checkPostCode();
		}, 300);
	}
	else {
		$('#nearest_store').html('some content');
		return;
	}
}
checkPostCode();

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.