loriensleafs
loriensleafs

Reputation: 2255

delaying function before it launches not working

I'm trying to delay a function that I have in a jquery script from launching. I put in a settimeout but it isn't taking for some reason, it breaks the rest of the script. Thanks in advance for any help I can get on this.

JS

<script type="text/javascript">
    $(function () {
        $("#fullscreen_launch").click(function () {
            $("#fullscreen").animate({
                height: '100%',
                top: '0px',
            }, 950, setTimeout(function () {
                $('#fullscreen').load('http://www.klossal.com/portfolio/space_fullscreen.html');
            }, 2000);
            });
        });
    });
</script>

Upvotes: 0

Views: 62

Answers (2)

Jonathan Payne
Jonathan Payne

Reputation: 2223

I got your code working, it's located here: http://jsfiddle.net/VEXfJ/7/

A good way to step through your code is to add alert statements.

$(function()
{
    $("#fullscreen_launch").click(function()
    {
        alert("Clicked");

        $("#fullscreen").animate(
        {
            top: 0,
            height: 'toggle'
        }, 5000, function()
        {

            alert("animate complete");

            setTimeout(function()
            {
                alert("timeout");
                $('#fullscreen').load('http://www.klossal.com/portfolio/space_fullscreen.html');
            }, 2000);
        });
    });
});​

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227200

The last parameter to animate is a callback. setTimeout doesn't return a function, it returns a timeoutID (used for clearTimeout). You need to pass a function to animate.

$(function () {
    $("#fullscreen_launch").click(function () {
        $("#fullscreen").animate({
            height: '100%',
            top: '0px',
        }, 950, function () {
            setTimeout(function () {
                $('#fullscreen').load('http://www.klossal.com/portfolio/space_fullscreen.html');
            }, 2000);
        });
    });
});

Upvotes: 2

Related Questions