2 Replies - 425 Views - Last Post: 11 January 2013 - 02:32 PM

#1 TGEMdead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 13

window.setTimeout problem

Posted 11 January 2013 - 02:04 PM

First of all, I am not sure if what I am trying to do even works and yes, I do suck at html and javascript.

My problem is that I can not get my timeout to work as I want it to work. I have googled for it for a long time and all I get is how I use it for something that does not do anything else. I want my button to first change the text (which works perfectly fine if I erase the link) and then after 2 seconds link to another address...

I am not sure if it is an error on my html or js file so I will show both.

Code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="html.css" />
<title>Sinister Darkness</title>
</head>
<body>
<h1 id="h1">Greetings stranger</h1>
<button onclick="h1()" input type="button" value="Redirect">Leave this page and enter a huge encyclopedia</button>
<script src="html.js"></script>
</body>
</html>

function h1() {
	document.getElementById("h1").innerHTML="Have a nice day!";}
	window.setTimeout("function h1()",2000);
	function h1() {window.location = "http://www.wikipedia.org/"
	}



I have tried to change the
window.setTimeout("function h1()",2000);
to
window.setTimeout("h1()",2000);
and
window.setTimeout("h1",2000);
but niether worked.

Thanks in advice for your answer.

Is This A Good Question/Topic? 0
  • +

Replies To: window.setTimeout problem

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,575
  • Joined: 12-December 12

Re: window.setTimeout problem

Posted 11 January 2013 - 02:18 PM

You have two functions and an element named h1. This is a bad idea, particularly because h1 is also a tag name. Show a little imagination!

Anyway,

function h1() {
    document.getElementById("h1").innerHTML = "Have a nice day!";
    window.setTimeout(function () {
        window.location = "http://www.wikipedia.org/";
    }, 2000);
}


[I'm assuming this code is in 'html.js'.]
Was This Post Helpful? 1
  • +
  • -

#3 TGEMdead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 13

Re: window.setTimeout problem

Posted 11 January 2013 - 02:32 PM

View Postandrewsw, on 11 January 2013 - 02:18 PM, said:

You have two functions and an element named h1. This is a bad idea, particularly because h1 is also a tag name. Show a little imagination!

Laughed so hard, because it's true xD

Anyway, this worked perfectly, Thanks a lot! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1