Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make an ajax call using jQuery every second?
    text
    copied!<p>Okay, so I managed to get everything running - one last thing. Basically, one jQuery+ajax function adds 15 seconds to the date and adds a new row to MySQL. I need to put in a new row so I can get the last 5 rows for the history:</p> <pre><code>&lt;?php include("inc/dblink.inc"); $itemid = intval($_POST['i']); $bid = intval($_POST['b']); $row = mysql_fetch_array(mysql_query("SELECT * FROM test WHERE itemid=$itemid ORDER BY bidid DESC LIMIT 0,1")); $date = $row['date']; $newdate = strtotime($date); $newerdate = ($newdate + 15); $newestdate = date("Y-m-d H:i:s", $newerdate); mysql_query("INSERT INTO test (date,bid,itemid) VALUES ('$newestdate','$bid','$itemid')"); ?&gt; </code></pre> <p>The second script refreshes every second and displays the data from the table.</p> <pre><code>&lt;script type="text/javascript"&gt; var auto_refresh = setInterval( function() { $('#timeleft').load('gettime.php', { i: &lt;?=$itemid;?&gt; }).show(); $.ajax({ type: "POST", url: "gettime.php", data: ({i : &lt;?=$itemid;?&gt;}), success: function(data){ var s = data; var t = s.split(/[- :]/); var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); $('#defaultCountdown').countdown({until: d,compact: true, description: ''}); } }); }, 1000); &lt;/script&gt; </code></pre> <p>Here's gettime.php</p> <pre><code>&lt;?php include("inc/dblink.inc"); $itemid = intval($_POST['i']); $row = mysql_fetch_array(mysql_query("SELECT * FROM test WHERE itemid='$itemid' ORDER BY bidid DESC LIMIT 0,1")); $date = $row['date']; echo $date; ?&gt; </code></pre> <p>And I also used Keith Wood's jQuery countdown script found in <a href="http://keith-wood.name/js/jquery.countdown.js" rel="nofollow">http://keith-wood.name/js/jquery.countdown.js</a></p> <p>The problem:</p> <p>This line <code>$('#timeleft').load('gettime.php', { i: &lt;?=$itemid;?&gt; }).show();</code> is good. The new time with 15 seconds added shows up without a problem. It's the countdown I'm having issues with. It doesn't add 15 seconds to the timer but if I refresh the whole page, I can see that 15 seconds was added.</p> <p>What am I doing wrong? Fairly new with jQuery. Thanks!</p> <p>You can see it in action <a href="http://angkas.ph/testarea/index.php" rel="nofollow" title="here">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload