possible java script question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I have a .net application that shows the start page for a few seconds and
then goes to another start page. I was wondering if it would be possible to
put a count on the page to let the user know how much time is left before it
goes to the other page, thanks.
 
<input type="text" size="10" id="timer" value="60">
<input type="text" size="5" id="timer" value="60" style="font-size: 14pt"
name="timer">
<script type="text/javascript"><!--
var timeLeft = 60;
function setTimer()
{
if (timeLeft > 0) timeLeft--;
document.getElementById("timer").value = timeLeft;
}
setInterval("setTimer()", 1000);
//--></script>

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Write to the web page using a JavaScript function that is invoked every
second (using the setInterval), like this:

<script language="javascript">
var iSeconds =30; //wait for 30 seconds
var iIntervalID = null;
function StartCounter()
{
iIntervalID = setInterval("WriteCount()",100);//writes the count every 1
second
}

function WriteCount(){

iSeconds--;
var DisplayBox = document.getElementById("RemainingSeconds");
DisplayBox.innerText="Time remaining: " + iSeconds;
if (iSeconds==0)
{
window.clearInterval(iIntervalID);
}

}

</script>
</head>
<body onload="StartCounter()">
<div id="RemainingSeconds" style="font-size:16pt;color:black;border:1pt gray
solid;width:200pt;background-color:lightsteelblue;"/>
</body>
 
To expand on everyone else's solutions for doing a countdown, I would
suggest that you have JavaScript redirect when the timer reaches zero. If
you use the meta refresh, then that may be out of synch with the JS timer
variable and may not be exact. Just a thought.
 
Hi thanks I tried it but get the following error,
per the active schema the element input can not be nested within "head"
--
Paul G
Software engineer.


Kevin Spencer said:
<input type="text" size="10" id="timer" value="60">
<input type="text" size="5" id="timer" value="60" style="font-size: 14pt"
name="timer">
<script type="text/javascript"><!--
var timeLeft = 60;
function setTimer()
{
if (timeLeft > 0) timeLeft--;
document.getElementById("timer").value = timeLeft;
}
setInterval("setTimer()", 1000);
//--></script>

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Hi thanks for the response, I was able to add the script but the count did
not seemed to showup on the page.
 
Hi Paul,

You might have to check for syntax mistakes in specifying the id of the
object that displays the message on your page.

This is the complete HTML file of the code I posted earlier. If you save it
and browse it, it should work fine.

<html>
<head>
<script language="javascript">
var iSeconds =30; //wait for 30 seconds
var iIntervalID = null;
function StartCounter()
{
iIntervalID = setInterval("WriteCount()",100);//writes the count every 1
second
}

function WriteCount(){

iSeconds--;
var DisplayBox = document.getElementById("RemainingSeconds");
DisplayBox.innerText="Time remaining: " + iSeconds;
if (iSeconds==0)
{
window.clearInterval(iIntervalID);
}

}

</script>
</head>
<body onload="StartCounter()">
<div id="RemainingSeconds" style="font-size:16pt;color:black;border:1pt gray
solid;width:200pt;background-color:lightsteelblue;"/>
</body>
</html>
 
Hi Paul,

The text box was an example of displaying the data. The data is the variable
"timeLeft." The function decrements the variable and displays it (in this
case) in a text box. How you display the value of the variable is up to you.
In other words, I didn't intend for you to simply copy and paste it.
Apparently, you pasted it into the <head> section of your document. I'm not
sure why. In any case, an "input type=text" text box belongs inside a form.
A script can be anywhere in an HTML document.

Just use that Software engineer inside you to take my example, and create
your own implementation of it, based upon the principles illustrated, and of
course, obeying the rules of HTML.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

Paul said:
Hi thanks I tried it but get the following error,
per the active schema the element input can not be nested within "head"
 
ok got it working!
--
Paul G
Software engineer.


Kevin Spencer said:
Hi Paul,

The text box was an example of displaying the data. The data is the variable
"timeLeft." The function decrements the variable and displays it (in this
case) in a text box. How you display the value of the variable is up to you.
In other words, I didn't intend for you to simply copy and paste it.
Apparently, you pasted it into the <head> section of your document. I'm not
sure why. In any case, an "input type=text" text box belongs inside a form.
A script can be anywhere in an HTML document.

Just use that Software engineer inside you to take my example, and create
your own implementation of it, based upon the principles illustrated, and of
course, obeying the rules of HTML.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
ok got it working!

:-D

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top