1

I want to change the content of a div 3 times, with a delay of 1 second between each change. delay() doesn't work in this code :

$('#import-status-msg').html('Importing content ..').delay(1000).
html('Organizing content ..').delay(1000).
html('Eating burrito ..');

I get 'Eating burrito' directly. What's the shortest way to get this done?

4 Answers 4

2

Using the setTimeout-function:

setTimeout(function() {
    $('#import-status-msg').html('Importing content ..');
    setTimeout(function() {
        $('#import-status-msg').html('Organizing content ..');
        setTimeout(function() {
            $('#import-status-msg').html('Eating burrito ..');
        }, 1000);
    }, 1000);
}, 1000);

You can't use jQuery's delay()-function as that only has an effect on the standard effects queue eg. animations.

Sign up to request clarification or add additional context in comments.

Comments

1

a more general solution, using jquery's queue function:

var gen = function (str, delay) {
  return function (n) {
    $(this).html(str);
    setTimeout(n, delay || 1000);
  };
};

$('#import-status-msg').queue([
  gen('Importing content ..'),
  gen('Organizing content ..'),
  gen('Eating burrito ..')
]);

http://jsbin.com/oraler/1/

3 Comments

Can you explain whats happening in the setTimeout function line here?
using queue, the callbacks will be called with a next-function (the n parameter). This function will either do nothing (end of queue) or call the next callback in the queue. setTimeout is just do delay moving to the next cb in the queue.
Thanks, its very comprehensive and educative!
0

Use:

setTimeout(function(){},1000)

Comments

0
var q = ['Importing content ..','Organizing content ..','Eating burrito ..'];
$("#test").html(q[0]);
var i = 1;
var p = setInterval(function(){
    $("#test").html(q[i]);
    i++;
    if(i==q.length){
        clearInterval(p);
    }
},1000);

fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.