Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33

    Problema con countdown multiplo

    Ciao a tutti sono nuovo in questo forum. Ho cercato quasi da due giorni la soluzione ad un problema che si verifica per un contdown in javascript.
    Praticamente se inserito singolarmente in una pagina, funziona correttamente.
    Se invece lo inserisco in un'altra pagina che richiama più volte il countdown non va.
    Non so se posso postare qui il codice.
    Il link del sito è http:// coupon . masebo .it

    Spero qualcuno possa aiutarmi.
    Grazie

    codice:
     <script type="text/javascript">  dateFuture = new Date('<?php echo date("D M d Y H:i:s", strtotime($row["coupon_enddate"])); ?>'); //pass the date format similar to date function  function gettimes(){ 	var url = '/gettimestamp.php'; 	$.post(url,function(e){  		document.getElementById("generaldate").innerHTML=e; 	});   }  gettimes();url_redirect(DOCROOT);  function GetCount(){  	gettimes(); //alert(document.getElementById("generaldate").innerHTML); 	var dateNow = new Date(document.getElementById("generaldate").innerHTML); //grab current date 	var amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates 	delete dateNow;  		var days=0;var hours=0;var mins=0;var secs=0;var nodays="";var nohrs="";var nomins="";  		var amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs  		var days=Math.floor(amount/86400);//days 		 		var days = days*24; 		 		var amount=amount%86400;  		var hours=days + Math.floor(amount/3600);//hours 		 		var amount=amount%3600;  		var mins=Math.floor(amount/60);//minutes 		 		var amount=amount%60;  		var secs=Math.floor(amount);//seconds  		if(days == 0 && hours == 0 && mins == 0 && secs == 0 ) 		{	 			window.location='<?php echo DOCROOT; ?>';exit; 		}  		/*if(days != 0) 		{ 		nodays = days +((days!=1)?"":""); 		//alert(nodays); 		document.getElementById('tot_days2').innerHTML=nodays; 		} 		else 		{ 		document.getElementById('tot_days2').innerHTML='';		 		document.getElementById('days').innerHTML='';		 		}*/ 		  		if(days != 0 || hours != 0) 		{  		nohrs = hours +((hours!=1)?"":""); 		if(nohrs < 10 && nohrs >= 0){nohrs = '0'+ nohrs;} 		//alert(nohrs); 		document.getElementById('tot_hrs2').innerHTML=nohrs;		 		} 		else 		{ 		document.getElementById('tot_hrs2').innerHTML='';		 		document.getElementById('hrs').innerHTML='';		 		} 				 		if(days != 0 || hours != 0 || mins != 0) 		{ 		nomins = mins +((mins!=1)?"":""); 		if(nomins < 10 && nomins >= 0){nomins = '0'+ nomins;}		 		//alert(nomins); 		document.getElementById('tot_mins2').innerHTML=nomins;				 		} 		else 		{ 		document.getElementById('tot_mins2').innerHTML='';		 		document.getElementById('mins').innerHTML='';		 		}		  		if(secs < 10 && secs >= 0){secs = '0'+ secs;}		 		document.getElementById('tot_secs2').innerHTML = secs;   		setTimeout("GetCount()", 1000); 		 }  window.onload=GetCount;//call when everything has loaded  </script>

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Gli id devono essere univoci se ripeti questo codice per tutte le offerte
    codice:
    <div align="CENTER">
    <span id="tot_hrs2">21</span>
    <span id="hrs"></span>
    <span id="tot_mins2">46</span>
    <span id="mins"></span>
    <span id="tot_secs2">09</span>
    </div>
    è ovvio che funziona solo con la prima devi legare l'orario (count-down) all'offerta lato server (php) tipo id="tot_hrs2_<?php echo row["cid"]; ?>" cosi da renderlo univoco e anche l'orario (se non è già) dovrà essere impostato dal server.


    P.S. Benvenuto sul forum di html.it
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33
    Ciao e grazie mille per la risposta.
    Ho fatto come mi hai consigliato:
    <span id="tot_hrs2_<?php echo $row["coupon_id"];?>"><?php echo $default_hr; ?></span>

    Ho collegato l'id dell'offerta e adesso sembra essere cambiato qualcosa, ma il contdown non parte.


  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33
    adesso i primi due non funzionano mentre l'ultimo va in contdown.
    Ho aggiunto gli id anche in alto nello script
    vicino ad ogni valore
    tot_hrs2
    tot_mins2
    tot_secs2

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33
    ho risolto da solo con un'altra soluzione. Grazie

  6. #6
    potresti mostrarla anche a me? Anche io ho il problema con countdown multipli, in pratica va l'ultimo con un numero n volte maggiore di decrementi (con due righe scatta di due secondi). Il codice che ho usato è questo.

    Anche se faccio diversi assegnamenti non va, credo perché la classe richiama comunque se stessa (purtroppo di js non so molto e mi fa anche abbastanza schifo).

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33
    Ciao,
    è un lavoro che ho fatto un pò di tempo fa.
    Se riesco a recuperarlo... ti mostro il tutto ed anche la classe utilizzata.

  8. #8
    te ne sarei molto grato, è un lavoro che devo consegnare a breve e non ci salto fuori

  9. #9
    Utente di HTML.it
    Registrato dal
    Dec 2011
    Messaggi
    33
    1) creo e passo le variabili data

    Codice PHP:
    <?php 
    //get coopen end date to show Countdown Timer

    $data_timer explode('-',$row["annuncio_enddate"]);
    $data_timer1 explode(' ',$data_timer[2]);
    $data_timer2 explode(' ',$data_timer1[1]);
    $data_timer3 explode(':',$data_timer2[0]);
    ?>
    2) carico lo script

    codice:
    <script>
    function CD_M(strTagId){var objMeta=document.getElementsByTagName("meta");if(objMeta&&objMeta.length){for(var i=0;i<objMeta.length;++i){if(objMeta.item(i).scheme==strTagId){var name=objMeta.item(i).name;var content=objMeta.item(i).content;if(name.indexOf("mindigits")>0||name.indexOf("hidezero")>0){window[strTagId][name]=parseInt(content,10)}else {window[strTagId][name]=content}}}}};function CD_UD(strContent,objW){objW.node.innerHTML=strContent};function CD_T(strTagId){var objNow=new Date();var objW=window[strTagId];if(objW.msoffset){objNow.setMilliseconds(objNow.getMilliseconds()+objW.msoffset)};CD_C(objNow,objW);if(objW.intEvntDte<=objNow.valueOf()){if(objW.event_functionhandler&&typeof window[objW.event_functionhandler]=="function"){window[objW.event_functionhandler](new Date(objW.intEvntDte));objW.event_functionhandler=""}if(objW.event_msg||objW.event_redirecturl){var msg="<span id=\""+strTagId+"_complete\">"+objW.event_msg+"</span>";if(objW.event_redirecturl){location.href=objW.event_redirecturl}else if(objW.event_audio_src){var strMimeType=objW.event_audio_mimetype;var audioObject="<object style=\"visibility:hidden;\" id=\"MediaPlayer\" width=\"2\" height=\"2\" data=\""+objW.event_audio_src+"\" type=\""+strMimeType+"\"></object>";CD_UD(msg+audioObject,objW)}else {CD_UD(msg,objW)};return}};setTimeout("if(typeof CD_T=='function'){CD_T(\""+strTagId+"\")}",1100-objNow.getMilliseconds())};function CD_C(objNow,objW){var intMS=objW.intEvntDte-objNow.valueOf();if(intMS<=0){intMS*=-1};var intD=Math.floor(intMS/864E5);intMS=intMS-(intD*864E5);var intH=Math.floor(intMS/36E5);intMS=intMS-(intH*36E5);var intM=Math.floor(intMS/6E4);intMS=intMS-(intM*6E4);var intS=Math.floor(intMS/1E3);var strTmp=CD_F(intD,"d",objW)+CD_F(intH,"h",objW)+CD_F(intM,"m",objW)+CD_F(intS,"s",objW);CD_UD(strTmp,objW)};function CD_F(intData,strPrefix,objW){if(intData==0&&objW[strPrefix+"_hidezero"]){return ""};var strResult=""+intData;var intMinDigits=objW[strPrefix+"_mindigits"];if(intData.toString().length<intMinDigits){strResult="0000000000"+strResult;strResult=strResult.substring(strResult.length,strResult.length-intMinDigits)}if(intData!=1){strResult+=objW[strPrefix+"_units"]}else {strResult+=objW[strPrefix+"_unit"]};return objW[strPrefix+"_before"]+strResult+objW[strPrefix+"_after"]};function CD_Parse(strDate){var objReDte=/(\d{4})\-(\d{1,2})\-(\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{0,2})\s+GMT([+\-])(\d{1,2}):?(\d{1,2})?/;if(strDate.match(objReDte)){var d=new Date(0);d.setUTCFullYear(+RegExp.$1,+RegExp.$2-1,+RegExp.$3);d.setUTCHours(+RegExp.$4,+RegExp.$5,+RegExp.$6);var tzs=(RegExp.$7=="-"?-1:1);var tzh=+RegExp.$8;var tzm=+RegExp.$9;if(tzh){d.setUTCHours(d.getUTCHours()-tzh*tzs)}if(tzm){d.setUTCMinutes(d.getUTCMinutes()-tzm*tzs)};return d}else {return NaN}};function CD_Init(){var strTagPrefix="countdown";var objElem=true;if(document.getElementById){for(var i=<?php echo $row["coupon_id"];?>;objElem;++i){var strTagId=strTagPrefix+i;objElem=document.getElementById(strTagId);if(objElem&&(typeof objElem.innerHTML)!='undefined'){var strDate=objElem.innerHTML;var objDate=CD_Parse(strDate);if(!isNaN(objDate)){var objW=window[strTagId]=new Object();objW.intEvntDte=objDate.valueOf();objW.node=objElem;objW.servertime="";objW.d_mindigits=1;objW.d_unit=" day";objW.d_units=" days";objW.d_before="";objW.d_after=" ";objW.d_hidezero=0;objW.h_mindigits=2;objW.h_unit="h";objW.h_units="h";objW.h_before="";objW.h_after=" ";objW.h_hidezero=0;objW.m_mindigits=2;objW.m_unit="m";objW.m_units="m";objW.m_before="";objW.m_after=" ";objW.m_hidezero=0;objW.s_mindigits=2;objW.s_unit="s";objW.s_units="s";objW.s_before="";objW.s_after=" ";objW.s_hidezero=0;objW.event_msg="";objW.event_audio_src="";objW.event_audio_mimetype="";objW.event_redirecturl="";objW.event_functionhandler="";CD_M(strTagId);if(objW.servertime){var objSrvrTm=CD_Parse(objW.servertime);if(isNaN(objSrvrTm)){objElem.innerHTML=strDate+"**";continue}else {objW.msoffset=parseInt((objSrvrTm.valueOf()-(new Date()).valueOf())/1000,10)*1000}}else {objW.msoffset=0};CD_T(strTagId);if(objElem.style){objElem.style.visibility="visible"}}else {objElem.innerHTML=strDate+"<a href=\"/\" title=\"CountdownPro Error:Invalid date format used,check documentation (see link)\">*</a>"}}}}}if(window.attachEvent){window.attachEvent('onload',CD_Init)}else if(window.addEventListener){window.addEventListener("load",CD_Init,false)}else {window.onload=CD_Init};
    </script>

    3) Stampo il coutdown tramite span associandolo al id del annuncio.

    codice:
     <span style="font-size:14px;">Mancano solo...
    </span><span style="font-size:20px; color:#B5366E;"><span id="countdown<?php echo $row["annuncio_id"];?>"><?php echo $data_timer[0]; ?>-<?php echo ($data_timer[1]) ; ?>-<?php echo $data_timer1[0]; ?> <?php echo $data_timer3[0]; ?>:<?php echo $data_timer3[1]; ?>:<?php echo $data_timer3[2]; ?> GMT+02:00</span>
          </span>

    Tutta la documentazione la trovi al seguente link: http://andrewu.co.uk/clj/countdown/pro/


    Spero possa esserti stato di aiuto.
    Buon lavoro

    Ciauz!!!

  10. #10
    ciao, grazie mille per lo script, ho usato la versione "easy" dato che non avevo grandi pretese, funziona abbastanza bene ma succedono cose strane se la data viene superata!

    Ad esempio con la data 2012-06-05 06:00:00 GMT+02:00

    mi restituisce (a quest'ora): 34 giorni 06 ore 09 minuti 14 secondi

    che non capisco come lo calcoli...tu hai avuto lo stesso problema?


    EDIT: ho capito cosa fa...calcola in avanti!! Mi dice quanti giorni sono passati dalla fine...provo a sistemare.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.