The jQuery Way™ is to use delay
and queue
:
$('#SC')
.html('LOADING')
.delay(1000)
.queue(function (n) {
$(this).html('<img src="secure/captcha/securimage_show.php" class="img_middle" />');
n();
});
It helps maintain chainable code, which can tend to be more readable. Readable code is easier to maintain and extend.
The vanilla JavaScript way is to use a callback in setTimeout
:
$('#SC').html('LOADING');
setTimeout(function () {
$('#SC').html('<img src="secure/captcha/securimage_show.php" class="img_middle" />');
}, 1000);
Chaining setTimeout
calls within setTimeout
calls can end up with callback arrow code, so I generally recommend making use of queueing even if you're not using jQuery. You'd want to make use of some sort of queueing.
The following example makes use of a Queue
class that I wrote:
(function () {
var sc,
q;
q = new Queue();
sc = document.getElementById('SC');
sc.innerHTML = 'LOADING';
q.queue(function (n) {
setTimeout(n, 1000);
},function (n) {
sc.innerHTML = '<img src="secure/captcha/securimage_show.php" class="img_middle" />';
n();
});
}());