1

Is there a way to use setTimeout in this ajax call. This is my code:

jQuery.ajax({
    type    : "POST",
    url     : dir+"all/money/myFile.php",
    data    : "page="+data.replace(/\&/g, '^'),
    success : function(msg) {
    var info = jQuery('.product_overview #allinfo').html();
    var url = '<a href="http://www.mySite.com/openMe/letmeview.php?preview='+msg+'.html" target="_blank" class="check_link_preview" rel="'+msg+'.html">Check Preview</a>';          jQuery('.option_additional').next().find('textarea:first').text(info+url);
                },
    complete: function() {
    jQuery('.add-to-cart button.btn-cart').delay(500).trigger('click');
}
});

I want to do something before this ajax will be triggered that is why I'll use setTimeout or something that would delay this action.

How would I do that?

Thanks in advance :)

6
  • So if you want to use it - why didn't you just do that?! ;-) Commented Aug 8, 2011 at 6:38
  • @zerkms: How would I put it in a setTimeout function? Commented Aug 8, 2011 at 6:39
  • Can you show me an example please using my code above. Thanks :) Commented Aug 8, 2011 at 6:44
  • uhm, have you ever used setTimeout? Commented Aug 8, 2011 at 6:44
  • 1
    any reason for that? "Won't work" is not an explanation of an issue. Commented Aug 8, 2011 at 6:47

4 Answers 4

2

Haven't used jquery with setTimeout before but try

var t = window.setTimeout(function, delay);

replace function in the above code with your jquery function.

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

Comments

2

In a complete function:

complete: function () {
  setTimeout(function () {
     // your action here
  }, 500);
}

Comments

1

You can use beforeSend, Example

$(function() {

    function callBeforeAjax() {
        alert('and now do ajax');
    }

    $.ajax({
        beforeSend: callBeforeAjax,
        type: "POST",
        url: "/",
        data: "",
        success: function(msg) {},
        complete: function(msg) {
            alert(msg);
        }
    });
});

Refer this

6 Comments

Uhm, cannot find delay property. Where have you taken it from?
Also it should be beforeSend: callBeforeAjax, not beforeSend: callBeforeAjax()
You don't need the delay property, coz the beforeSend will be called before the Ajax. I have updated my answer without the delay method, but I will add the reference now.
lol, you had to put the reference to that plugin at first then. PS: I'm not the OP ;-)
:) yeah, probably had to hey! LoL
|
1

@Tols is right, it works. Try something like this: setTimeout(function(){jQuery('.add-to-cart button.btn-cart').trigger('click');}, 500);

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.