Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Jquery Ajax setTimeout problem

    I work on a shoutbox script.I send the data with AJAX,it goes into the db,then if it's successful i call a second Ajax function,sending a variable to my PHP page,this way ordering a refresh.The problem is that the second call is too quick,so it won't display the last message.I want to delay the second Ajax,but i can't figure out how.I tried setTimeout,but i get the function undefined message,or my browser froses,i tried the delay() function but i don't know where should i take it,i always get error messages.
    The relevant code is:
    PHP Code:
    $(document).ready(function(){
        $(
    "#mybutton_2").click(function(){
            $(
    "#newtext").val("");
        });
        
        $(
    "#mybutton").click(function(){
        var 
    newText = $("#newtext").val();
            
    newText jQuery.trim(newText);
            if(
    newText.length >= 1)
            {
                $.
    ajax({
                    
    type:"POST",
                    
    url:"sendit.php",
                    
    cahe:false,
                    
    data:"text=" newText,
                    
    success:function(html) {
                        
                        $.
    ajax({
                            
    type:"POST",
                            
    url:"shouts.php",
                            
    cahe:false,
                            
    data:"res=ok",
                            
    success:function(){
                            
                            }
                        })
                    }
                });
                
            }    
        });


  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Why not have the sendit.php return what shouts.php returns? What is the reasoning to make a second call to the server when sendit.php just inserts it into the database?

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    In addition to Skywalker's good suggestion, I don't think this is spelled right:

    PHP Code:
    cahe:false

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you Skywalker2208,you are perfectly right,i'll do that.
    Thank you Fumigator for showing me the typo.


  •  

    Posting Permissions

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