• 0

Get users CURRENT time, NOT SERVER time


Question

Hey guys,

so this is my test site.

www.helixagent.com/test7.php

www.helixagent.com/time.php

I made this script that gives me the users current time. It works pretty well by itself.

but i'm trying to load it into a DIV using an AJAX call along with jQuery.

so this is my jQuery bit

var auto_refresh = setInterval(

function(){

$('#timeDate').load('time.php');

}, 5000);

and then i have a <div id="timeDate"></div> in my index page where I want the page loaded and regressed every 5 sec.

the time.php isn't being loaded into the DIV... I think its because I have a Javascript running in time.php...

can anyone let me know what i'm doing wrong?

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

You've got a javascript error on the page. You need to ensure that jQuery is available. I'd also recommend you not creating your auto_refresh even until after the page has fully loaded:

$(function() {
  setTimeout(function() {
    $("#timeDate")
      .fadeOut("slow")
      .load("time.php")
      .fadeIn("slow");
  }, 3000);
});

The problem you are having with the script is that you are using: "jquery.1.4.js", the versioning names of the jquery libraries are formatted, such like: "jquery-{ver}[-min].js", so the version you should be pointing at is "jquery-1.4.js", which is available at http://www.helixagent.com/js/jquery-1.4.js.

Link to comment
Share on other sites

  • 0

oh, hmmm well i got the links corrected.

but the problem still there.

one thing that I am able to do is echo out date() function and that will actually load into my div. also if i place the javascript directly into the div without being triggered by the ajax then that will still work.

it only has this issue when the external file contains the javascript. this won't be loaded by the ajax call.

hmmm, so any insight on this? or maybe a better way of showing the current user time to the visitor dynamically?

p.s. thanks for the heads up on the link problem

Link to comment
Share on other sites

  • 0

You are using AJAX and PHP to display the time on a web page? Is it performing something server-side every 5 seconds that I'm missing, or are you actually making the biggest mistake anyone can make with AJAX?

If it's the latter, calculate the time and present it with javascript alone.

Link to comment
Share on other sites

  • 0

if you access my time.php then you will see Javascript in action.

if you access the test7.php it has an auto refresh DIV in place with the AJAX and jQuery doing its magic.

What I was saying is that since the jQuery is loading time.php it won't work like that. UNLESS if that time.php was pure PHP

then it would work without a hitch, but it will load the time server side. so i made the javascript so that it grabs the users client side time.

but independently they work just fine, but not if the jquery tries to load the time.php with the jquery load function then it won't display the javascript output.

in the designated DIV...

you see what i'm saying?

test7 with the auto refresh div

time.php with the js

oh and if you notice, if you access time.php directly you see something like Monday 29th March 2010 3:02 AM but if its accessed by test7.php via the ajax call then you don't get the entire string to load.

oh oh, and the reason for this is because i have the time displayed on the page and and i want to update ever x seconds so that the minutes will update accordingly so it won't show the same time all the... time

Link to comment
Share on other sites

  • 0

i don't want the page to refresh... i have a jquery tabs thing going on so refreshing the page will reset my tabs and whatnot...

this is the reason for this...

Link to comment
Share on other sites

  • 0

Think about what you are doing, it makes no sense to be asking the server anything when javascript is more than capable of operating on datetimes all on its own.

What's more, with a javascript-only solution you can decrease the update interval to 1 second, making it perform like an actual clock.

http://www.google.ie/search?q=javascript+clock

Link to comment
Share on other sites

  • 0

You can update content without using ajax and without refreshing the page.

http://dfizz.com/date/

oh nice, I guess I was so caught up with some design elements that I over looked the simplicity of it all.

hey, your clock show me AM instead of PM, is that a bug in your code or is there a 12 hour difference somewhere?

Link to comment
Share on other sites

  • 0

oh nice, I guess I was so caught up with some design elements that I over looked the simplicity of it all.

hey, your clock show me AM instead of PM, is that a bug in your code or is there a 12 hour difference somewhere?

never mind, i fixed it.

we can mark this thread solved!

thanks guys for your awesome help ;)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.