Welcome to WebmasterWorld Guest from 54.211.17.91

Forum Moderators: open

Message Too Old, No Replies

Holding a Javascript Button

Have a new page appear up after holding a button down for 10 seconds

   
11:36 pm on Mar 1, 2006 (gmt 0)

5+ Year Member



Hey, I'm trying to figure out how to get a new page of my site to appear (in a different window) when a button is held down for 10 seconds. I'm pretty sure this can be done with javascript, just don't know how. Any thoughts? Thanks.
12:34 am on Mar 2, 2006 (gmt 0)

5+ Year Member



Start a for loop when your link (or button) is pressed, or the cursor hovers over it, then you can tell it to execute once your loop until it reaches the number. In this case, the number would be how many seconds.

There is probably a more efficient way, but this will work.

NOTE: for( int x = 0; x < 10; ++x ) won't work because 10 would be reached a lot quicker than in 10 seconds.

gph

12:54 am on Mar 2, 2006 (gmt 0)

10+ Year Member



you could use setTimeout


function chk(up){
if(up)clearTimeout(chk.timer);
else chk.timer=setTimeout('window.open("page.htm")',10000)
}

<input type="button" value="hold 10 sec" onmousedown="chk()" onmouseup="chk(1)">

4:09 am on Mar 2, 2006 (gmt 0)

5+ Year Member



Thanks, works great :)

One more question on a different idea using javascript. Is there a way to make it so that when you click a button (not hold down), it starts a timer counting down from 10, and when the timer hits 0, a link appears below (that can direct the user to another page). Or, alternativly, a link is already on the page, and just can't be used till the timer hits 0. Either of these will work, just looking for the easiest way to do it though.

gph

3:04 am on Mar 3, 2006 (gmt 0)

10+ Year Member



Is there a way to make it so that when you click a button (not hold down), it starts a timer counting down from 10, and when the timer hits 0, a link appears below (that can direct the user to another page).

Yes, set the link to display:none or visibility:hidden then use setTimeout onclick in the button to show the link.

7:25 pm on Mar 4, 2006 (gmt 0)

5+ Year Member



[quote]Yes, set the link to display:none or visibility:hidden then use setTimeout onclick in the button to show the link.[quote]
I understand what you are saying, but I'm not so good with javascript as to be able to actually write that. Could someone give me an example script please?

gph

7:45 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



What you want to do isn't very hard to learn

[w3schools.com...]


function showLink(){
setTimeout('document.getElementById("link").style.display="inline"',10000)
}

<input type="button" value="showLink()" onclick="showLink()">

<a href="#" style="display:none" id="link">link</a>

9:40 pm on Mar 4, 2006 (gmt 0)

5+ Year Member



Thanks :)

I'll look over W3 again too (Have forgotten alot of javascript since the last time I had a refresher because I havn't been using it much).