Javascript auto refresh code

Discussion in 'JavaScript' started by shivampaw, Nov 17, 2013.

  1. #1
    I am not sure how to make this code auto refresh every 8 seconds, please can you point me in the right direction or show me a code.

    CODE:
    
    <?php
      $query = mysql_query("SELECT * FROM users WHERE username='$user' AND admin='1'");
      $numrows = mysql_num_rows($query);
      ?>
      <li><a href='chat.php'><span>Chat <?php if($numrows == "1"){echo "<b><font color='red'>(!)</b></font>";} ?></span></a></li>
    
    Code (markup):
    Thanks
     
    shivampaw, Nov 17, 2013 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Why don't you just use a meta refresh?

    
    <meta http-equiv="refresh" content="8;URL='chat.php'" />
    
    Code (markup):
     
    MrLeN, Nov 17, 2013 IP
  3. shemseddine

    shemseddine Active Member

    Messages:
    144
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    55
    #3
    I guess you don't have much experience, try w3schools for javascript. I think you should look into ajax also.

    I would give you the code but since, I'm new it's not letting me paste the code.

    EDIT - Actually didn't think of that. The person above me just made it a lot easier.
     
    shemseddine, Nov 17, 2013 IP
  4. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    I only want to lead only the code in the starting post, they code is in a div called menu.

    I know that this requires Ajax but not sure how to to do it
     
    shivampaw, Nov 17, 2013 IP
  5. rehan.khalid.9235

    rehan.khalid.9235 Active Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    85
    #5
    So you want to refresh a particular 'div' after every 8 seconds. you can try this algorithm

    this is Javascript Code
     function refreshdiv (){
       
        $.ajax({
          url: '',
          type: 'json',
          success: function (r) {
            $('#div_id').html(r.contents);
          }
        });
    
    }
    
    setInterval(refreshdiv() ,8000);
    HTML:
    in place of url, you need to put path of file/function (php) where you perform action and return the values.
     
    rehan.khalid.9235, Nov 17, 2013 IP
  6. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    SOLVED:

    var auto_refresh = setInterval(
    function ()
    {
    $('#cssmenu').load('lol.php');
    }, 5000); // refresh every 5 seconds
    Code (markup):
    And then
    <body onload="auto_refresh()";>
    Code (markup):
    This fixed my problem :)

    Quote this for best answer!
     
    shivampaw, Nov 17, 2013 IP
  7. leeku

    leeku Well-Known Member

    Messages:
    246
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    well done !!
     
    leeku, Jan 8, 2015 IP