javascript code, what´s bad here?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

javascript code, what´s bad here?

Post by visonardo »

it would show "actualiza en xx segundos." where appear <div id="redirect2"></div> but dont work, what is bad here?

Code: Select all

<div id="redirect2"></div>

<script language="JavaScript">
// segundos para refresh
var countdownfrom=100;
var currentsecond=document.getElementById("redirect2").innerHTML="Actualiza en "+(currentsecond+1)+" segundos.";
//var currentsecond=document.getElementById("redirect2").innerHTML=currentsecond+1;
function countredirect(){
	if (currentsecond!=1){
		currentsecond-=1;
		document.getElementById("redirect2").innerHTML="Actualiza en "+currentsecond+" segundos.";
		//document.getElementById("redirect2").innerHTML=currentsecond;
	}else{
		document.location.reload();
	return
	}
	setTimeout("countredirect()",1000);
}
countredirect();
</script>
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

"Don't work" can mean many different things. Are you getting any javascript warnings or errors?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

nickvd wrote:"Don't work" can mean many different things. Are you getting any javascript warnings or errors?
no, just that dont show that i want, what is bad?

for example, it would show first "Aactualiza en 100 segundos."

then its change to 99, then to 98 and thus, when is 0 the page is reloaded
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

This line

Code: Select all

var currentsecond=document.getElementById("redirect2").innerHTML="Actualiza en "+(currentsecond+1)+" segundos.";
is a little strange. What is its intended purpose? Currently, it's setting current second to "Actualiza....". Later, you're trying to add one to it.

<edit>
Since I'm in a nice mood :) The following

Code: Select all

var countdown = 7;

function countredirect(){
   if (countdown >= 1){
      document.getElementById("redirect2").innerHTML="Actualiza en " + countdown + " segundos.";
      countdown -= 1;
   } else {
      document.location.reload();
   }
   setTimeout("countredirect()",1000);
}
countredirect();
works just fine...
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

nickvd wrote:This line

Code: Select all

var currentsecond=document.getElementById("redirect2").innerHTML="Actualiza en "+(currentsecond+1)+" segundos.";
is a little strange. What is its intended purpose? Currently, it's setting current second to "Actualiza....". Later, you're trying to add one to it.

<edit>
Since I'm in a nice mood :) The following

Code: Select all

var countdown = 7;

function countredirect(){
   if (countdown >= 1){
      document.getElementById("redirect2").innerHTML="Actualiza en " + countdown + " segundos.";
      countdown -= 1;
   } else {
      document.location.reload();
   }
   setTimeout("countredirect()",1000);
}
countredirect();
works just fine...
yes, youare right. i didnt remember that the value where i wont keep this string is used again. Thank you nickvd
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

Code: Select all

<div id="redirect2"></div>

<script language="JavaScript">
// segundos para refresh
var countdownfrom=60
var currentsecond=countdownfrom+1;
document.getElementById("redirect2").innerHTML="Actualiza en severals segundos.";</script>
the <div id=""></div> dont show that must :S

i did this easy to start to see what happen because dont work the mine
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Javascript = Client Side.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

does someone know what is bad in my code? 8O
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you're missing a colon on line 1: "var countdownfrom=60;"

you should use setInterval() instead of setTimeout.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

Thank to all, the problem was that i was testing that just in a txt for example, the tag <script ..> was not between heads tags.
Post Reply