3

Guys I have a data coming from an outsource table which I'm presenting in table. The original data coming in Json which I'm decoding using PHP function and displaying.

I have been googling for auto refreshing that data and found this Auto refresh table without refreshing page PHP MySQL

I have managed to built what is suggested in the Ticked answers and seems like working for the first time but it doesn't refresh. So when page is loaded

I'm calling this

$(document).ready (function () {
                    var updater = setTimeout (function () {
                        $('div#user_table').load ('get_new_data', 'update=true');
                        /*  location.reload(); */
                    }, 1000);
                });

Which loads a function named get_new_data from controller file and loads on to

 <div class="container-fluid" id="user_table">

                       </div>

Codeigniter controller function is

 public function get_new_data(){

    $url = 'https://gocleanlaundry.herokuapp.com/api/users/';
    $result = $this->scripts->get_data_api($url);
    $data_row = $result;


                            $tbl    = '';
                            $tbl .= '<table class="table table-striped table-responsive" id="tableSortableRes">
                               <thead>
                                   <tr>
                                       <th>Name</th>
                                       <th>Email Address</th>
                                       <th>Role</th>
                                       <th>Status</th>
                                       <th>Action</th>
                                   </tr>
                               </thead>
                               <tbody>';

                                foreach($data_row as $row){
                                $tbl .= '<tr class="gradeX">
                                       <td>' . $row->name . '</td>
                                       <td>' . $row->email . '</td>
                                       <td>' . $row->role . '</td>
                                       <td><p id="' . $row->_id. '_status">'.( $row->alive == true ?  'Active' : 'Banned').' </p></td>
                                       <input id="' . $row->_id . 'status_val" value="' . $row->alive . '" type="hidden">
                                       <td class="center">

                                      <a href="#" id="' . $row->_id . '_rec" onclick="UpdateStatus(' . $row->_id . ')" class="btn btn-mini ">'
                                       . ($row->alive == '1' ? '<i class="fa fa-thumbs-o-up fa-fw"></i>' :  '<i class="fa fa-thumbs-o-down fa-fw"></i>' ) . '</a>
                                       </td>

                                   </tr>
                               ';
                                }
                                $tbl .= '</tbody>
                           </table>';

                                echo $tbl;

}

As I said when page loads is showing data so something is working but when I add new data to the database is doesn't automatically show up. I want function to call and refresh the data. Sorry I'm very new to javascript and jQuery so please go easy on me :)

Thank you guys

5
  • 1
    The settimeout fonction is called on l'y once. If you want your refresh function WAT called every 1000 ms, you should use setinterval function in place of settimeout. Commented Jul 6, 2015 at 6:06
  • Yes, but it's still not showing the new data Commented Jul 6, 2015 at 6:06
  • @SébastienDoncker thanks man but I'm really new on Jquery things can you give me an example or rewrite my code? please Commented Jul 6, 2015 at 6:09
  • I don't really want it to load every sec. 4 times a minute would do for me too. I just want new data pulling in automatically Commented Jul 6, 2015 at 6:11
  • Yeahay I googled the setInterval and it's working for me :).... @SébastienDoncker you are a star Commented Jul 6, 2015 at 6:13

1 Answer 1

3

To refresh every 15 secondes (4 times a minute), you should use setInterval function in place of setTimeout.

Your code will be like this:

$(document).ready (function () {
    var updater = setInterval (function () {
        $('div#user_table').load ('get_new_data', 'update=true');
    }, 25000);
});

EDIT: But if you want to be sure to not overload your page and, as you ask, to load first the data then refresh regularly. You have to do the following :

  1. load your data the first time.
  2. When it's finished, call a setTimeout to refresh in 25 secs.
  3. When your page is refreshed, call a SetTimeout to refresh it in 25 secs.
  4. and again, and again.

This is better than using setInterval. To do that, you have to use the complete callback of jQuery load function http://jqapi.com/#p=load .

function refresh() {
    setTimeout(function() {
          $('div#user_table').load ('get_new_data', 'update=true', refresh);
    }, 25000);
}

$(document).ready (function () {
    $('div#user_table').load ('get_new_data', 'update=true', refresh);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that, one more question. How to load first and then call interval. What's happening with above is it's taking to long to show the data on page load
@Zee I just edit my answer to show you how to do that.

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.