Countdown timer

The user clicks, a countdown appears on the page, when the time has passed, you can click again. If the time has not passed, displays the message “please wait”.

Tell me how to implement. Preferably a piece of code. I’m just learning, I don’t know much yet. Thanks in advance.

And yet, you only need to use php.


Answer 1, authority 100%

<button id="start">    :)</button>
<span id="timerValue"></span>
<div id="result">...</div>
<script type="text/javascript">
    var timerInterval; //    
    onload = function() {
        var start = document.getElementById("start");
        start.onclick = timerStart;
    }
    function timerStart() {
        //   ...
        //   this, ..       ....
        this.innerHTML = "...";
        timerInterval = setInterval("timerAction()", 1000);
    }
    function timerAction() {
        var timerValue = document.getElementById("timerValue");
        if(timerValue.innerHTML == "")  {//   ...   
             timerValue.innerHTML = 30;
        }
        else if(timerValue != "0"){
             //    ....
             var seconds = parseInt(timerValue.innerHTML);
             timerValue.innerHTML = seconds - 1; //  ...
        }
        else {
            // ,    :)
            //   "" ...
            removeInterval(timerInterval);
            //    '...'
            var result = document.getElementById("result");
            result.innerHTML = '';
            //    -   ...
            var button = document.getElementById("start");
            button.innerHTML = "...";
            //         div result...
            var link = document.createElement("link");
            link.href = "http://somePage.html"; // href  ...
            link.innerHTML = "  ...";
            result.appendChild(link);
            //           ...
            button.onclick = ""; //      :)
            return;
        }
    }
</script>


If I understood correctly, then something like this…
Perhaps there are some errors somewhere, but if so, they are minor…


Answer 2

Um… Maybe I’m not a pro, of course, but I think it’s impossible to do this only in php… no, you can, but then you need the same number of pages as numbers + a bunch of redirects. This can be done (and it will be easier) in JS. I didn’t understand JS, so I can’t say anything. Here is link, I will be glad if it helps somehow.


Answer 3

© the fire is kindled

Once, any file

:
<iframe src="/getlink.php?linkid=15" style="width: 200px;height: 40px;"></iframe>

Two, getlink.php

$links = array(); //    ,   ,   
$timeout = 5;// sec
if (!empty($links[@$_GET['linkid']])) {
  @ob_end_clean();
  echo 'wait ';
  @flush();
  for ($i = $timeout; $i > 0; $i--) {
    sleep(1);
    echo $i.' ';
    @flush();
    }
  echo '<a href="'.$links[@$_GET['linkid']].'">your link</a>';
  @flush();
  } else die('link not found');

Plain PHP, by the way.


Answer 4

Fixed:

session_start();
$timer = 10 - (time() - $_SESSION['time']);
if($timer > 0){
    echo " ".$timer." ";
}else{
    ?><a href="?start=true">   </a><?
}
// 
if($_GET['start'] && $timer <=0){
$_SESSION['time'] = time();
}