rib3ye
rib3ye

Reputation: 2923

Why doesn't setTimeout work as callback?

Basically, I'm trying to fade in #container_swap over #container, wait two seconds, then swap the background of #container and finally fade #container_swap out.

function swapBg(){
    $('#container').css('background-image',newBg)
    $('#container_swap').hide();
}
$(
    function(){
        $('#container_swap')
            .css('background-image',newBg)
            .fadeIn(2000)
        ,setTimeout(swapBg(), 2000)
    }
)

Upvotes: 0

Views: 35

Answers (1)

Blender
Blender

Reputation: 298146

You're calling swapBg and passing its result to setTimeout. You want to pass the function instead:

setTimeout(swapBg, 2000)

Upvotes: 2

Related Questions