|
|
|
Yazan
|
Konu: countdown (Okunma Sayısı 530 defa)
|
tayfun_de
PHP Öğrencisi
Offline
Mesaj Sayısı: 104
|
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 <?php
$end_datetime = '2007-10-22 16:47:00';
$target = strtotime("$end_datetime"); $today = time (); echo $difference = ($target-$today);
?>
ajax_time_counter.js 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 <html> <head> <title></title> </head> <body> <P>Countdown ends:</P> <script src="ajax_time_counter.js"></script> </body> </html>
|
|
|
|
|
Logged
|
|
|
|
engin
Admin
Offline
Mesaj Sayısı: 495
|
|
|
|
|
|
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
|
şö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
Mesaj Sayısı: 104
|
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 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 <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 <?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
|
|
|
|
|
 |
|