Forum Moderators: open

Message Too Old, No Replies

Keep alert going

         

1simone

5:02 am on Apr 12, 2007 (gmt 0)

10+ Year Member



Hello, I have this below which causes an alert after 10 seconds. How would I keep the alert going every 10 seconds after the first time? Please let me know, thank you very much.

<script type="text/javascript">
//If the time is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.

var d = new Date();
var time = d.getSeconds();

if (time > 10)
{
alert("Hello day!")
}
</script>
</body>
</html>

music_man

6:41 am on Apr 12, 2007 (gmt 0)

10+ Year Member



Hi there,

Check out: [w3schools.com...] (a very useful website)

So let's apply this to your situation:


<script type="text/javascript">
function showAlert() {
var d = new Date();
var time = d.getSeconds();
if (time > 10)
{
alert("Hello day!")
}
var t = setTimeout("showAlert()",10000) // in milliseconds
}
</script>

and

<a href="#" onclick="showAlert()">Start loop</a>

When you click, it activates the function. I'm not entirely sure why you want it to loop though...

1simone

8:10 am on Apr 12, 2007 (gmt 0)

10+ Year Member



Thank you, yes thats where I got the first one W3.

I need to happen after the page loads though not on a button click. So I tried yours without the button and nothing happened. Can someone help? Thanks.

Dabrowski

10:33 am on Apr 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For a repeating event use setInterval instead of setTimeout. setInterval is persistent, setTimeout is only once.

<script type="text/javascript">
function showAlert() {
var d = new Date();
var time = d.getSeconds();
if (time > 10)
{
alert("Hello day!")
}
}

var t = setInterval("showAlert()",10000) // in milliseconds

function stopBotheringMe() {
clearInterval( t);
}
</script>

Also as we have set the interval outside the function it will now start automtically.

If you wanted to you could use stopBotheringMe() to stop the interval.

This seems like a particularly annoying thing to do, do you mind if I ask what it's for?

1simone

10:57 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



Thankyou, I'm just trying to learn how to control an alert without a button or link so it will just appear on the screen. Once I can do that maybe I can time other things someday. I checked w3 and nothing has helped me keep the alert going. The setInterval makes sense, didn't know about it, all I ever saw was setTimeout. I tried the just above code but its not working yet. Hope someone can show me the just above working code. Thanks.

1simone

2:12 am on Apr 13, 2007 (gmt 0)

10+ Year Member



I tried this, still not working.
Anyone know how to keep an alert going?
Thank you very much.

<script type="text/javascript">
function showAlert() {
var d = new Date();
var time = d.getSeconds();
if (time > 10)
{
alert("Hello day!")
setInterval("showAlert()",10000)}
}
</script>

Dabrowski

1:53 pm on Apr 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You appear to have copied the code wrong. The setInterval line should NOT be inside the function, but after it.

That way the browser will execute it as soon as it comes across it. If you put the line inside the function as your last post showed it won't work until it's called by something else.