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();
Like it.? Share it:

Comments are closed.