SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: How to delay

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post How to delay

    Hi,

    I am having trouble how do i use the delay before sending a request,what i want is to delay about 3secs to show my loading bar and make a request to server.

    i tried this
    beforeSend:function() {
    <div><img src="loadingbar.gif"></img></div>.delay(3); //3 seconds.
    }


    Thank you in advance

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Unfortunately, you cannot call delay() on random HTML

    In this case, just use setTimeout:

    Code JavaScript:
    function doAjax(){
      // Server request here
    }
     
    $("myElement").on("click", function(){
      setTimeout(doAjax, 3000);
    });

    Hope that helps.

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Unfortunately, you cannot call delay() on random HTML

    In this case, just use setTimeout:

    Code JavaScript:
    function doAjax(){
      // Server request here
    }
     
    $("myElement").on("click", function(){
      setTimeout(doAjax, 3000);
    });

    Hope that helps.



    Hi Pullo, Thank you so much it's working

    I have some question about when do we use beforeSend() ?..i thought this is use by some website that i had seen that will display something like this "please wait...." when we submit some forms to the server...so in my point of view of this did they also used setTimeout ?please correct me if i am wrong.

    Thank you

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi jemz,

    The problem here is the asynchronous nature of JavaScript.

    If you do something like:

    Code JavaScript:
    $.ajax({
      type: "GET",
      url: "test.php",
      beforeSend: function(){
        img.fadeIn(2000);
      },
      success:function(res)  {
        $("#result").html(res);
      }
    });

    The jQuery will call the beforeSend function, before making the GET request, but it will not wait for it to finish.

    HTH

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you pullo for enlighten my mind


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •