Merhaba, Ziyaretçi. Lütfen giriş yapın veya üye olun.
Aktivasyon mailiniz gelmediyse buraya tıklayın.
05, 2008, 08:59:13 am
42712 Mesaj 9331 Konu Gönderen: 17904 Üye
Son üye: RafiX
Turk-Php.Com Forum  |  Yardım Forumları  |  HTML & JavaScript & XML Forumu  |  AJAX  |  countdown « önceki sonraki »
Sayfa: [1] Yazdır
Yazan Konu: countdown  (Okunma Sayısı 530 defa)
tayfun_de
PHP Öğrencisi
*
Offline Offline

Mesaj Sayısı: 104


Üyelik Bilgileri
countdown
« : 22, 2007, 11:30:48 am »

merhaba,

3 script ten olusan ve geri kalan zamani (dakika:saniye) gösteren bir deneme var.
burda yapmak istedigim zaman bilgisinin ajax yardimi ile serverden alinip javascript icinde isleme konmasi.
sayfa ilk acildiginda her sey güzel calisiyor. ama benim asil yapmayi istedigim belirli araliklarla ajax server zamanini
yeniden okumasi ve javascripte aktarmasi.

yardimlariniz icin tesekkürler

ajax_time_counter.php
Kod:
<?php

$end_datetime 
'2007-10-22 16:47:00';


$target strtotime("$end_datetime");
$today time ();
echo 
$difference = ($target-$today);

?>


ajax_time_counter.js
Kod:
document.writeln('<span class="" style="font-size:14px;font-weight:bold;" id="countdowntimer"></span>');

function UpdateTimer() {
 TimeLeft--;
 strText = ''
 if (TimeLeft <= 0) {
  strText = "NOW!!!";
 }
  else {
   strText = ""+parseInt(TimeLeft/60%60)+ ":"+parseInt(TimeLeft/1%60)+ "";
  }
   document.getElementById('countdowntimer').innerHTML = strText;   
}

var TimeLeft = -1;
InitiateTimer();

function InitiateTimer() {

 var http;
 var http = false;
 if (window.XMLHttpRequest) { // Mozilla, Safari,...
  http = new XMLHttpRequest();
  if (http.overrideMimeType) {
   http.overrideMimeType('text/xml');
  }
 }
 else if (window.ActiveXObject) { // IE
  try { http = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) {
   try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
   catch (e) {}   
  }
 }
 
 if (!http) {
  alert('error :( ');
  return false;
 }
 
 http.open("GET", "ajax_time_counter.php", true);
 http.onreadystatechange = function() {
  if (http.readyState == 4) {
    TimeLeft = http.responseText;
    setInterval("UpdateTimer()",1000);
  }
}
http.send(null);
}

ajax_time_counter.html
Kod:
<html>
<head>
<title></title>
</head>
<body>
<P>Countdown ends:</P>
<script src="ajax_time_counter.js"></script>
</body>
</html>


Logged
engin
Admin
*
Offline Offline

Mesaj Sayısı: 495



Üyelik Bilgileri
Ynt: countdown
« Yanıtla #1 : 23, 2007, 12:35:20 am »

Logged

[color=3399CC]Engin Dumlu[/color][/i]
[color=3399CC]engin ~ turk-php.com[/color]
[color=3399CC]achilles ~ member.turk-php.com[/color]
PHP5 Hosting
obareey
PHP Stajyeri
**
Offline Offline

Mesaj Sayısı: 246



Üyelik Bilgileri
Ynt: countdown
« Yanıtla #2 : 23, 2007, 03:55:13 pm »

şöyle birşey yapabilirsin:

UpdateTimer içine bir sayaç koyarsın ve istediğin değere ulaştığında yeniden InıtiateTimer fonksiyonunu çağırırsın. burada dikkat etmen gereken şey UpdateTimer Interval'inin temizlenmemesi. bu nedenle UpdateTimer fonksiyonunu AJAX cevabı geldikten sonra bir kez çağırıp, bu fonksiyonun içinde eğer senin istediğin sayaç değerine ulaşmamışsa setTimeout ile yeniden bu fonksiyonu kurarsın, eğer istediğin değere ulaşmışsa InitiateTimer fonksiyonunu çağırırsın...

kolay gelsin...
Logged
tayfun_de
PHP Öğrencisi
*
Offline Offline

Mesaj Sayısı: 104


Üyelik Bilgileri
Ynt: countdown
« Yanıtla #3 : 24, 2007, 08:01:02 am »

cevaplariniz icin herkese tesekkürler.

benim ulastigim cözüm.
gizli hidden alani kullanmak her nekadar optimal bir cözüm olmasa bile :-)

ajax_time_countdown.js
Kod:
document.writeln('<span class="" style="font-size:14px;font-weight:bold;" id="countdowntimer"></span>');

function UpdateTimer() {

  TimeLeft=document.getElementById('h_time').value
  TimeLeft--;
  document.getElementById('h_time').value = TimeLeft;
 
  strText = ''
  if (TimeLeft <= 0) {
   strText = "NOW!!!";
  }
   else {
    strText = ""+parseInt(TimeLeft/60%60)+ ":"+parseInt(TimeLeft/1%60)+ "";
   }   
     document.getElementById('countdowntimer').innerHTML = strText;
}


window.setInterval("InitiateTimer()",10000);


function InitiateTimer() {
 
  var http;
  var http = false;
 
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http = new XMLHttpRequest();
    if (http.overrideMimeType) {
      http.overrideMimeType('text/xml');
     }
  }
  else if (window.ActiveXObject) { // IE
    try { http = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) {}   
    }
  }
 
  if (!http) { alert('error :( '); return false; }
 
   http.onreadystatechange = http_return;   
   
   http.open("POST", "ajax_time_countdown.php", true);
   
   
   function http_return() {
    if (http.readyState==4) {
     if (http.status == 200) {
     
      xmldoc = http.responseXML;
      rootnode = xmldoc.getElementsByTagName('root').item(0);
     
     
      document.getElementById('h_time').value = rootnode.firstChild.data;
     
     }
     else if(http.status == 404) { alert(":-("); }
     else { alert("Error has occurred with status code:   "+http.status); }
    }
   }
}

ajax_time_countdown.html
Kod:
<html>
<head>
<title>ajax_time_countdown</title>
<script src="ajax_time_countdown.js"></script>
</head>
<body>
<P>Countdown ends:</P>

<form method='POST'> <input type='hidden' name='h_time' id='h_time' value='1'> </form>
<script language='JavaScript'> window.setInterval('UpdateTimer()',1000); </script>

</body>
</html>


ajax_time_countdown.php
Kod:
<?php
header 
("content-type: text/xml");
echo '
<?xml version="1.0" encoding="utf-8"?>
';
echo '<root>';



$end_datetime = '2007-10-23 17:47:00';
$target = strtotime("$end_datetime");
$today = time ();
echo $difference = ($target-$today);
echo '</root>';
?>
A
« Son Düzenleme: 24, 2007, 08:03:36 am Gönderen: tayfun_de » Logged
Sayfa: [1] Yazdır 
« önceki sonraki »
Gitmek istediğiniz yer:  


Turk-Php.Com Forum | SMF Forum Software © 2005, Simple Machines LLC. All Rights Reserved.