1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Ajax loop updater

Discussion in 'HTML & JavaScript' started by qlithe, May 26, 2017.

  1. qlithe

    qlithe Power Member

    Joined:
    Feb 14, 2012
    Messages:
    657
    Likes Received:
    96
    Hey

    I need a way to constantly update my users balance and update a column in my database called "last activity". I'm considering something like this:
    Code:
        $(document).ready(function(){
      
            function update()
            {
                $.ajax({
                        type:"post",
                        url:"update.php",
                        data:formData,
                        dataType: 'text',
                        success:function(data){
                      
                          // get balance from mysql
    
                          // update column in mysql
    
                        },
                        complete:function(){
                            setTimeout(update, 5000);
                        },
                        global: false
                });
            }
          
            setTimeout(update, 5000);
          
        });
    Are there any downsides to this method? Will it be very resource heavy?

    Thanks in advance
     
  2. SergeiVL

    SergeiVL Newbie

    Joined:
    Thursday
    Messages:
    5
    Likes Received:
    1
    Gender:
    Male
    The decision is good.
    Note: Calculate only the new balance value on the server side.
     
  3. blackh4t

    blackh4t Registered Member

    Joined:
    Jan 30, 2009
    Messages:
    78
    Likes Received:
    29
    Yes looks good, but might result in big MySQL load if traffic is high, so make sure your server can handle.
     
  4. roki4ka

    roki4ka Senior Member

    Joined:
    Jun 20, 2016
    Messages:
    869
    Likes Received:
    637
    Hmm you may have problem with high loads on the server