Jump to content

Recommended Posts

I've looked all over but the scripts I find don't do this or are very bloated.

 

I simply have a PHP unix time, which I want to countdown from in seconds as a visual display (no refresh).

 

so if:

$time = 20;//20 seconds in UNIX Time?

 

How do I countdown from 20?

 

Many thanks,

Nick.

Link to comment
https://forums.phpfreaks.com/topic/135376-javascript-countdown-from-php-timestamp/
Share on other sites

it would look something like this:

<html>
  <head>
    <script type="text/javascript">
      var num = 20;
      var timer;
      function countdown() {
        num--;
        document.getElementById('countdown_ele').innerHTML = num;
        if(num < 1){
          clearTimeout(timer);
        }
      }
      function init() {
        document.getElementById('countdown_ele').innerHTML = num;
        timer = setInterval("countdown()",1000);
      }
    </script>
  </head>
  <body onload="init();">
    <span id="countdown_ele"></span>
  </body>
</html>

Brilliant! Thanks! I have some logic issues though, with what I'm trying to do.

 

When you enter a page, it records that exact time with time() and enters it in the database (as $viewTime);

 

What I wish to do, is do a 20 second countdown, from when you entered that page but it only works until the difference is greater than 20, then it gets confused... here is my code...

 

$timeDifa = time() - $viewTime;

$timeDif = 20 - $timeDifa;

echo $timeDif;

 

So if it was 1228329985 and the time now is 1228329990, that's 15 seconds but as soon as it goes over 20, it goes negative and gets confused. I want it to simply stop at 0 once it does this.

 

Any thoughts?

 

Thanks alot,

Nick.

Aha, of course! Finally, trying to make it display an alternate message, so when it reaches 0, it says "DONE". I'm not very confident with Javascript but this is what I tried:

 

    <script type="text/javascript">
	var num = <?=$timeDif?>;
	if(num <= 0){
		var num = "DONE";
	}else{
		var timer;
	}
	function countdown(){
		num--;
		document.getElementById('countdown_ele').innerHTML = num;
		if(num < 1){
			clearTimeout(timer);
		}
	}
	function init(){
		document.getElementById('countdown_ele').innerHTML = num;
		timer = setInterval("countdown()",1000);
	}
    </script>

 

Which works but then it seems to try counting again and displays "NaN" instead. Also, is there anyway to load the init() without placing it in the body tag?

 

Many thanks for your help! - Nick.

init() can't be run until the <span> is created. so you can just put it at the bottom of the page:

 

<html>
  <body>
  
  
    <span id="countdown_ele"></span>
    


    <script type="text/javascript">
      var num = <?=$timeDif?>;
      var timer;

      function countdown(){
         if(num < 1){
            if(timer)
               clearTimeout(timer);
            document.getElementById('countdown_ele').innerHTML = 'DONE';
         }else{
            document.getElementById('countdown_ele').innerHTML = num;
            num--;
            if(!timer)
               timer = setInterval("countdown()",1000);
         }
      }
      countdown();
    </script>
  </body>
</html>

Thanks a lot!

 

Is it possible to use javascript in a Form Button?

 

<input type="submit" name="submit" value="Enter" <script type="text/javascript"></script> />

 

What I'd like to do is make it so when it isn't "DONE" the form button is disabled. Then when it is DONE, it un-disables itself. I don't think though I can have javascript in a button from what my editor is telling me.

 

Any thoughts? I was thinking of simply attaching "disabled" to a Javascript variable in an IF statement. Is it this easy?

 

Thanks alot.

Had a quick Google for the answer, found something that might do the trick but doesn't seem to work. I'm trying to make the Submit button disable without refresh. This is what I tried:

 

    <span id="countdown_ele"></span><br /><br />
    <script type="text/javascript">
      var num = <?=$timeDif?>;
      var timer;

      function countdown(){
         if(num < 1){
            if(timer)
               clearTimeout(timer);
            document.getElementById('countdown_ele').innerHTML = '<font style=\'color:#355A75\'><b>Ready to Race!</b></font>';
        document.getElementById('idofmybutton').disabled = false; 
         }else{
            document.getElementById('countdown_ele').innerHTML = "<font style=\'color:#355A75\'><b>You must wait " + num + " seconds before you can race again.</b></font>";
        document.getElementById('idofmybutton').disabled = true; 
            num--;
            if(!timer)
               timer = setInterval("countdown()",1000);
         }
      }
      countdown();
    </script>

code worked for me:

 

    <span id="countdown_ele"></span>
    <input type="submit" id="idofmybutton" />
    <br /><br />

    <script type="text/javascript">
      var num = <?=$timeDif?>;
      var timer;

      function countdown(){
         if(num < 1){
            if(timer)
               clearTimeout(timer);
            document.getElementById('countdown_ele').innerHTML = '<font style=\'color:#355A75\'><b>Ready to Race!</b></font>';
        document.getElementById('idofmybutton').disabled = false;
         }else{
            document.getElementById('countdown_ele').innerHTML = "<font style=\'color:#355A75\'><b>You must wait " + num + " seconds before you can race again.</b></font>";
        document.getElementById('idofmybutton').disabled = true;
            num--;
            if(!timer)
               timer = setInterval("countdown()",1000);
         }
      }
      countdown();
    </script>

 

are you getting any JavaScript errors?

When you say loop through the elements do you mean like:

 

        document.getElementById('idofmybutton').disabled = false;

        document.getElementById('idofmybutton1').disabled = false;

        document.getElementById('idofmybutton2').disabled = false;

        document.getElementById('idofmybutton3').disabled = false;

 

etc? The problem though is the forms are generated from a PHP database, so there could be any amount of forms depending on how many rows are in the table.

no, something like this:

 

<style type="text/css">
  #countdown_ele {
    color: #355A75;
    font-weight: bold;
  }
</style>
<form id="myform" method="post" action="">
  <span id="countdown_ele"></span>
  <br><br>
  <input type="text" name="fiel1" /> <input type="button" value="Submit 1" /><br />
  <input type="text" name="fiel2" /> <input type="button" value="Submit 2" /><br />
  <input type="text" name="fiel3" /> <input type="button" value="Submit 3" /><br />

  <input type="submit" id="idofmybutton" />
</form>
<script type="text/javascript">
  var num = <?=$timeDif?>;
  var timer;

  function countdown(){
     if(num < 1){
        if(timer)
           clearTimeout(timer);
        document.getElementById('countdown_ele').innerHTML = 'Ready to Race!';
        setFormDisabed(false);
     }else{
        document.getElementById('countdown_ele').innerHTML = "You must wait " + num + " seconds before you can race again.";
        num--;
        if(!timer)
           timer = setInterval("countdown()",1000);
     }
  }
  function setFormDisabed( disabled ){
    var form = document.getElementById('myform');
    for(var n=0;n < form.length;n++){
      if(form[n].tagName == 'INPUT' && (form[n].type == 'button' || form[n].type == 'submit')){
        form[n].disabled = disabled;
      }
    }
  }
  
  setFormDisabed(true);
  countdown();
</script>

to do every form on the page:

  function setFormDisabed( disabled ){
    var forms = document.forms;
    for(var i=0;i < forms.length;i++){
      var form = forms[i];
      for(var n=0;n < form.length;n++){
        if(form[n].tagName == 'INPUT' && (form[n].type == 'button' || form[n].type == 'submit')){
          form[n].disabled = disabled;
        }
      }
    }
  }

or, for just every button on the page:

  function setFormDisabed( disabled ){
    var eles = document.getElementsByTagName('INPUT');
    for(var n=0;n < eles.length;n++){
      if(eles[n].type == 'button' || eles[n].type == 'submit'){
        eles[n].disabled = disabled;
      }
    }
  }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.