Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:16:59+00:00 2026-06-02T00:16:59+00:00

Okay, so I managed to get everything running – one last thing. Basically, one

  • 0

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:

<?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')");
?>

The second script refreshes every second and displays the data from the table.

<script type="text/javascript">
var auto_refresh = setInterval(
function()
{
$('#timeleft').load('gettime.php', { i: <?=$itemid;?> }).show();
$.ajax({
    type: "POST",
    url: "gettime.php",
    data: ({i : <?=$itemid;?>}),
    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);
</script>

Here’s gettime.php

<?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;
?>

And I also used Keith Wood’s jQuery countdown script found in http://keith-wood.name/js/jquery.countdown.js

The problem:

This line $('#timeleft').load('gettime.php', { i: <?=$itemid;?> }).show(); 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.

What am I doing wrong? Fairly new with jQuery. Thanks!

You can see it in action here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:17:00+00:00Added an answer on June 2, 2026 at 12:17 am

    There’s a couple of major issues here, and some minor ones.

    Date Format

    It’s much simpler to work with a unix timestamp rather than a formatted date. PHP and javascript are very similar in this regard. Both are based on the same “epoch”, 00:00 1 Jan 1970. UNIX/PHP work in seconds from that date and javascript works in milliseconds. Therefore a javascript Date object can be initialized with new Date(unix_timestamp * 1000); without the need for tricky string handling.

    Countdown

    Countdown is designed to free-run down to zero with a resolution of 1 second (then fire an optional callback function). With a poll interval of 1 second, it’s not necessary to reinitialize the countdown timer every time – only when the returned timestamp has changed.

    Efficiency

    With setinterval, it is kinder to client processors to create as many objects/strings as possible once outside the setinterval function. This particularly applies to jQuery statements with (static) selectors, as these cause the (potentially large) DOM to be searched.

    Putting all that together, my javascript would look like this:

    $(function(){
        var $$ = {//cached jQuery objects
            timeleft: $('#timeleft'),
            defaultCountdown: $('#defaultCountdown')
        };
        var map_1 = {//namespace for setInterval and countdown data
            url: 'gettime.php',
            lastTimeStamp: 0,
            countdown_settings: {until:null, compact:true, description:''},
            itemid: <?=$itemid;?>
        };
        var auto_refresh = setInterval(function() {
            $$.timeleft.load(map_1.url, {i:map_1.itemid}).show();
            $.ajax({
                type: "POST",
                url: map_1.url,
                data: {i:map_1.itemid},
                success: function(unixTimeStamp) {
                    unixTimeStamp = parseInt(unixTimeStamp);
                    if(unixTimeStamp !== map_1.lastTimeStamp) {
                        map_1.countdown_settings.until = new Date(parseInt(unixTimeStamp) * 1000);
                        $$.defaultCountdown.countdown(map_1.countdown_settings);
                        map_1.lastTimeStamp = unixTimeStamp;
                    }
                }
            });
        }, 1000);
    });
    

    Of course, to make this work, the PHP statement echo $date; will need to be modified to echo the unix timestamp representation of the date. That should be fairly trivial with knowledge of the date format as stored in the db.

    Edit: Unix epoch is 00:00 (midnight) 1 Jan 1970, not 12:00 (midday), doh! Javascript is the same so no negative affect on the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so yesterday I managed to get the latest trunk builds of NHibernate and
I've finally managed to get Jetty7 running as a service in Debian. Still, I'm
For a WP7 app I have managed to get a PeriodTask running as per
Okay, I've looked all over the internet for a good solution to get PHP
Okay, so I'm running a small test webserver on my private network. I've got
Okay, so I accidentally put the wrong remote URL into one of my git
Okay i've seen this done somewhere before where you have a function that takes
Okay so I've managed to read in a .txt file... now I'm trying to
Okay, I managed to create a match between two players. Now I will do
Okay so I've been set an assignment from university and I just can't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.