Solved

Need timmer code in java script

Posted on 2010-08-27
13
537 Views
Last Modified: 2012-05-10
I have time value in post like :
$ti=$_POST['ti']

and assume $ti equal to 5 min

i need some code which continuous show decreasing time like:
4.59 min after second 4.58 ........ so on

i have some code but it shows time in seconds and use input field...
please help..
0
Comment
Question by:savsoft
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 33544741

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
//<![CDATA[
var time = null;
window.onload = function() {
	timerDisplay = document.getElementById('timer');
	time = parseFloat(timerDisplay.innerHTML);
	time = isNaN(time)?5:time; // default time
	timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	setInterval(countdown, 1000);
}
function countdown() {
	if(time > 0.01) {
		time -= 0.01;
		if(time > 1 && time%1 > 0.59) time = Math.floor(time) + 0.59;
		timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	} else timerDisplay.innerHTML = "0:00";
}
//]]>
</script>
</head>

<body>
Time left: <span id="timer"><?= $ti ?></span>
</body>
</html>

Open in new window

0
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 total points
ID: 33544772
Sorry, logic error in previous code. Use this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
//<![CDATA[
var time = null;
window.onload = function() {
	timerDisplay = document.getElementById('timer');
	time = parseFloat(timerDisplay.innerHTML);
	time = isNaN(time)?5:time; // default time
	timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	setInterval(countdown, 1000);
}
function countdown() {
	if(time > 0.01) {
		time -= 0.01;
		if(time%1 > 0.59) time = Math.floor(time) + 0.59;
		timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	} else timerDisplay.innerHTML = "0:00";
}
//]]>
</script>
</head>

<body>
Time left: <span id="timer"><?= $ti ?></span>
</body>
</html>

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33545672
I'm sorry, but sam2912's code doesn't work entirely because it not allows to get $ti value. Using window.onload, your script run before $ti value is getted so countdown will start always from 5, the default value. I made a little modification posted in code snippet below.

Hope this helps

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
//<![CDATA[
var time = null;
function start() {
	timerDisplay = document.getElementById('timer');
	time = parseFloat(timerDisplay.innerHTML);
//	time = isNaN(time)?5:time; // default time // commented because not useful
	timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	setInterval(countdown, 1000);
}
function countdown() {
	if(time > 0.01) {
		time -= 0.01;
		if(time%1 > 0.59) time = Math.floor(time) + 0.59;
		timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	} else timerDisplay.innerHTML = "0:00";
}
//]]>
</script>
</head>

<body>
<?php
    if (isset($_REQUEST['ti'])){
        $ti = $_REQUEST['ti'];
    }else{
        $ti = 5;
    }
    echo "Time left: <span id='timer'>$ti</span>";
?>
    
<?php
echo "<br><br>INSOMMA $ti<BR>";
    echo "<script type='text/javascript'>start();</script>";
?>
</body>
</html>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33548426
Someone can tell me why in my test the sam2912's script didn't get $ti value and always countdfown started from 5?
This only because I would understand where is my error, if one error I made... Thanks
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33548467
<?= $ti ?> is a PHP variable printed into the span. It should be a positive number. Otherwise, the code will not be able to parse the value, and will take 5 as the default.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33548510
Thank,sam. But I wrote:

<?php
    if (isset($_REQUEST['ti'])){
        $ti = $_REQUEST['ti'];
    }
?>

leaving your code untouched. Then, if I typed http://localhost/countdown.php?ti=10 countdown didn't start from 10 but from 5. This is what I don't understand. I post the entire code I tried: it doesn't work. Bye
<?php
    if (isset($_REQUEST['ti'])){
      $ti = $_REQUEST['ti'];
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
//<![CDATA[
var time = null;
window.onload = function() {
	timerDisplay = document.getElementById('timer');
	time = parseFloat(timerDisplay.innerHTML);
	time = isNaN(time)?5:time; // default time
	timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	setInterval(countdown, 1000);
}
function countdown() {
	if(time > 0.01) {
		time -= 0.01;
		if(time%1 > 0.59) time = Math.floor(time) + 0.59;
		timerDisplay.innerHTML = time.toFixed(2).replace('.', ':');
	} else timerDisplay.innerHTML = "0:00";
}
//]]>
</script>
</head>

<body>
Time left: <span id="timer"><?= $ti ?></span>
</body>
</html>

Open in new window

0
 

Author Comment

by:savsoft
ID: 33548563
I have added <?php $ti;?>

in line no 17 of sam2912

time = isNaN(time)?<?php $ti;?>:time; // default time
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33548613
in that case you can remove these two lines of code:

        time = parseFloat(timerDisplay.innerHTML);
        time = isNaN(time)?5:time; // default time

and replace with

        time = <?= php $ti; ?>;
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33549096
Thank savsoft: this makes clear all things for me.
0
 

Author Comment

by:savsoft
ID: 33988975
i have some problem.

i want to send time at any instant by click on form submit button.

some thing like this:

<input type="hidden" name="instant_time" value="<span id="timer"><?= $ti ?></span>">


please help me...
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33989267
Honestly you should open a new question: two months are past from the closing this question, the problem is not the same (it's only related to the original one) and here you can't give points for new solution.

However, you can try this:

<input type="hidden" name="instant_time" value="<span id="timer"><?php= $ti ?></span>">

try

<input type="hidden" name="instant_time" value="<?php= $ti ?>">

Cheers
0
 

Expert Comment

by:savsofts
ID: 35039562
it doesn't work for less then 8 minutes
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question