Show Redirect In .. CountDown Timer PHP

I have this code so far that redirects the user after 5 seconds to the correct URL:

<?php $url = $_GET['url']; header("refresh:5;url=$url"); include('ads.php'); ?>

Please could you tell me how I can display a countdown timer talking about redirecting In .. with ... the number of seconds remaining. I am new to web development, so all the code will be useful!

+4
source share
4 answers
 <script type="text/javascript"> (function () { var timeLeft = 5, cinterval; var timeDec = function (){ timeLeft--; document.getElementById('countdown').innerHTML = timeLeft; if(timeLeft === 0){ clearInterval(cinterval); } }; cinterval = setInterval(timeDec, 1000); })(); </script> Redirecting in <span id="countdown">5</span>. 

You can try this.

+17
source

How is this a general question for beginners; I just wanted to emphasize that for best practice, setInterval should and can usually be avoided by using setTimeout recursively inside a function.

For instance:

 var timer = 5, el = document.getElementById('countdown'); (function t_minus() { 'use strict'; el.innerHTML = timer--; if (timer >= 0) { setTimeout(function () { t_minus(); }, 1000); } else { // do stuff, countdown has finished. } }()); 
+5
source

Great code from Kyle. I changed the timer by pressing the pause and resume buttons.

 <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var time_left = 50; var cinterval; var timestatus=1; function time_dec(){ time_left--; document.getElementById('countdown').innerHTML = time_left; if(time_left == 0){ clearInterval(cinterval); } } function resumetime() { //time_left = 50; clearInterval(cinterval); cinterval = setInterval('time_dec()', 1000); } function defaultstart() { time_left = 50; clearInterval(cinterval); cinterval = setInterval('time_dec()', 1000); } function stopstarttime() { if(timestatus==1) { clearInterval(cinterval); document.getElementById('stopbutton').value="Start"; timestatus=0; } else { clearInterval(cinterval); cinterval = setInterval('time_dec()', 1000); document.getElementById('stopbutton').value="Stop"; timestatus=1; } } defaultstart(); </SCRIPT> </HEAD> <body> Redirecting In <span id="countdown">50</span>. <INPUT TYPE="button" value="stop" id="stopbutton" onclick="stopstarttime()"> </body> </HTML> 
0
source

Here is my example, without variables outside the function. Depends on jQuery.

 function count_down_to_action(seconds, do_action, elem_selector) { seconds = typeof seconds !== 'undefined' ? seconds : 10; $(elem_selector).text(seconds) var interval_id = setInterval(function(){ if (seconds <= 0) { clearInterval(interval_id); if (typeof do_action === 'function') do_action(); } else $(elem_selector).text(--seconds); },1000) } 

Here is an example using http://jsfiddle.net/VJT9d/

0
source

Source: https://habr.com/ru/post/1333956/


All Articles