2011-06-23 6 views
0

時間、分、秒を示す私のWebページで動的な現在時刻を取得しようとしています。私はAjaxを使用していますが、これは動作していないようです。現在の時間はajaxが動作していない

また、私はSymfonyフレームワークを使用しています。ボディには

、私はその前に

<body onload="showCurrentTime()"> 

を得た:同じフォルダに

<script type="text/javascript" src="/js/curr_time.js"></script> 

curr_time.js

//Once the document is loaded, execute the showCurrentTIme function 
//This is the AJAX function that displays the current time on the screen. 
$(document).ready(function(){ showCurrentTime(); });  

function showCurrentTime() { 
    //Call the current time web service, 
    //grab the time off the server, and apply it to the 
    //end-user client after waiting for the document to load 

    $(document).ready(function() { 

     //Make the call to the XML web service 
     $.get("currentDateTime.php", function(currentDateTime) { 
      //Format the time returned by the server 
      var time = [ $("hour", currentDateTime).text(), 
      ":", 
      $("min", currentDateTime).text() ]; 

      //Determine how many milliseconds to will wait until 
      //the time needs to be refreshed again 
      var refresh = [(60 - $("sec", currentDateTime).text()) * 1000 ]; 

      //Display the time on the end-user client 
      $("#currentTime").html(time.join('')); 

      //Set a timer so that the time on the end-user client updates 
      // in sync with the server time to display the true current time 
      setTimeout('showCurrentTime()', refresh); 
     }); 
    }); 
} 

、私は、PHPファイルを持っているcurrentDateTime.php

<?php 
#Need to specify that this is an XML document in order for it to work with AJAX 
header('Content-Type: text/xml'); 

#Set variables equal to each part of the date and time 
$year = date("Y"); 
$mon = date("m"); 
$mday = date("d"); 
$hour = date("H"); 
$min = date("i"); 
$sec = date("s"); 

#Create the XML document of the current date and time 
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; 
echo '<currentDateTime>' . "\n"; 
echo "\t" . '<year>' . $year . '</year>' . "\n"; 
echo "\t" . '<month>' . $mon . '</month>' . "\n"; 
echo "\t" . '<day>' . $mday . '</day>' . "\n"; 
echo "\t" . '<hour>' . $hour . '</hour>' . "\n"; 
echo "\t" . '<min>' . $min . '</min>' . "\n"; 
echo "\t" . '<sec>' . $sec . '</sec>' . "\n"; 
echo '</currentDateTime>' . "\n"; 
?> 

と体で、

<p id="currentTime">--:--</p> 

私は長い時間が今のエラーを見つけようと、ない成功を収めてきました...

+2

エラーメッセージが表示されますか? Firebugなどを使って、Ajaxコールが投稿しているリクエストの内容とそれが返ってくる応答を調べましたか?あなたのPHPページに直接行くと予想されるコンテンツが返ってきたことを確認しましたか? –

+2

なぜ時間を得るためにサーバーに何度も電話する必要がありますか?私はあなたが時間を表示するか、[Date object](https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Date)を使うことができると思います。私がこれを尋ねるもう1つの理由は、(クライアント側で複製できるので)ほとんど利益を得るために、時間を稼ぐためにサーバーにたくさんの要求を送ることになるということです。また、MacでChromeとFFで作業しているようです(curr_time.jsの前にjQueryも含めていますか?) – Rob

+0

1)動作しています2)Robと同じように、あなたはjsクライアント側を使用することができます3)実際には、必ずしも訪問者のものと同じではないサーバー時間を取得しています。 –

答えて

1

あなたshowCurrentTime()機能掲示としては、一つだけを行います:$(document).ready()ハンドラを設定します。 文書から準備します(既に行っています)。あなたの関数内に別のハンドラを設定しないでください。次のようにちょうどその関数の内部から余分な$(document).ready()ものを取り除く:

function showCurrentTime() { 

    $(document).ready(function() { // DELETE THIS LINE 

     //Make the call to the XML web service 
     $.get("currentDateTime.php", function(currentDateTime) { 
      // rest of your function goes here... 
     }); 

    }); // DELETE THIS LINE 
} 

私はそれが一番の問題だと確信しています。

第2の問題は、refresh変数を作成するときに、数値ではなく1つの要素を持つ配列を指すように割り当てることです。角かっこを取り外します。また

、あなたのJSファイルに次の行を持つ:

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

あなたのHTML内のボディのonload:

<body onload="showCurrentTime()">

は冗長です。 1つを選んでください(JSファイルにあるものが望ましいです)。

関連する問題