TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

jQuery setTimeOut function not working

So the page starts with

<div id="show-quick-cart-zone"></div>

At the top of the page I use the load command script:

$('#show-quick-cart-zone').load('/loadbalance/quickcart');

This then inserts a cart div box onto the page like so:

<div id="quickcart" class="quickcart" style="display:none">
    <div class="quickcarttitle"><span>SHOPPING BAG</span></div>
    <strong>Total</strong> £<?=$CartTotal?><br />
    <a href="/cart?ref=quick-cart"><img src="secure-checkout.png"></a>
    <a onclick="jQuery('#quickcart').slideUp(500);" href="#close-quick-cart"><img src="continue-shopping.png" style="margin-top:8px"></a>
</div>

I use a normal hyper link to slideToggle it to appear on and off.

I want it so that if the user has the ?x=1 query in their URL, it preloads the box by sliding it down once loaded via load:

I have the following jQuery function. It basically should slideDown a cart div once the page has loaded.

<script type="text/javascript">
<? if($_GET["x"]=="1"){ ?>
function showCart() {
    $('#quickcart').slideDown(500);
    Cufon.replace('.quickcart');
}
// Toggle the Quick Cart (uses Load Balance for higher TPS no que!)
// showCart();
$(document).ready(function () {
    // Code to do stuff immediately
    setTimeout(showCart, 0);
});
<? } ?>
</script>

The box however does not slideDown with no errors. I want it so that if the user is on a normal page like so:

/cart/product.php

nothing happens.

However if they're on /cart/product.php?x=1

The box will slideDown as default indicating a new item added.

Upvotes: 0

Views: 235

Answers (1)

David Hellsing
David Hellsing

Reputation: 108500

The .load function takes some time to complete, probably longer time than the document takes to have the correct state. So try something like:

$('#show-quick-cart-zone').load('/loadbalance/quickcart', function() {
    // this will execute when the ajax load is complete
    showCart();
});

Upvotes: 1

Related Questions