0

I have this code on my page... the jQuery

window.setInterval( function(){
        $.get("php/get_posts.php", function(data) {
                 $('.post-container').prepend(data);
            });},10);

This is the get_posts.php

<?php
include('dbconnect.php');
session_start();
$uid= $_SESSION['uid'];
$get_ids=mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($get_ids)){
$id=$row['id'];
$sm=$row['message'];
}
$get_lpid=mysql_query("SELECT * FROM users WHERE uid='$uid'");
while($row_o = mysql_fetch_array($get_lpid)){
$l_pid=$row_o['lastviewed'];
}
if($id!=$l_pid){
    $insert=mysql_query("UPDATE users SET lastviewed='$id' WHERE uid='$uid' "); 
    if($insert){?>
           <div class='media'><img src='img/profile_pictures/thumbs/thumb_13718921232_119055628287843_1500172795_n.jpg' class='img-circle  post-circle pull-left'><div class='media-heading'><a href='#'>Pratik Sonar</a><div class='pull-right'><small>12.00PM</small></div></strong></div><div class='media-body'><?php echo $sm ?></div></div>
    <?php } else{
    }
  }
else{
}?>

This technique seems to work on every browser except chrome. I have tested ie, safari, firefox and opera all are working. Can anyone enlighten me on this thing? Is there something I don't know or am I missing?

Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • What error message are you getting. Anything in the console? – Zevi Sternlicht Jul 02 '13 at 11:26
  • Possibly `setInterval`, consider using `setTimeout` - http://stackoverflow.com/questions/6951727/setinterval-not-working-properly-on-chrome – Aaron W. Jul 02 '13 at 11:30
  • nothing in the console. It failed miserably in rest of the browsers also after using setTimeout. Is there anything to do with very little time allocated for script to run, i.e just "10/1000" of a second – Jay P. Sonar Jul 02 '13 at 12:42

3 Answers3

0

Try to wrap your code into this function:

$(document).ready(function() { ... });

Like:

$(document).ready(function() {
window.setInterval( function(){
        $.get("php/get_posts.php", function(data) {
                 $('.post-container').prepend(data);
            });},10);
});

You're probably better off using setTimeout() too.

Now the code runs when the DOM is fully loaded.

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
0

Why are you using window.setInterval? It's simply setInterval(), without any parent.

Try

$(document).ready(function() {
    setInterval(function(){
        $.get("php/get_posts.php", function(data) {
             $('.post-container').prepend(data);
        });
    },10);
});
Vii Systems
  • 80
  • 11
  • window.setInterval an setInterval will behave identically (unless there is a setInterval identifier introduced in scope). – Paul Jul 02 '13 at 16:42
0

Thank You guys for all your concerns. Well at last the bug got fixed by this chunk of code. I guess setTimeout gain gains victory over setInterval

$(document).ready(function() {
    window.setTimeout(function(){
        $.ajax({
        type: "GET",
        url: "php/get_posts.php",
        }).done(function( data ) {
            $('.post-container').prepend(data);
        });
        },10);
});