PDA

View Full Version : Javascript Timer



bevjones
12-23-2009, 07:37 AM
Hi people,

Im really stuck, I have a script I want to load when my page opens, I can et it to do this fine, but I want to be able to input a delay time, any time I want.

So my page loads, 1,2,3,4,5 then my script kicks in.

I have searched hi and low, but my understanding with JS is very basic, but I am willing myself to learn.

Thank you very much in advance for your help.

Bev

godofreality
12-23-2009, 08:01 AM
//format
<body onload="setTimeout('name_of_function()', time in milliseconds)">

//example
<script type="text/javascript">
function myfunc(){
alert('It took 5 seconds for this alert to appear, each second is 1000 in the setTimeout');
}
</script>
<body onload="setTimeout('myfunc()', 5000)">

Philip M
12-23-2009, 12:39 PM
bev - Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.

bevjones
12-23-2009, 06:17 PM
Sorry Philip, wont happen again,

Also godofreality, would i put something like this if I wanted my script to load 5 seconds after full page load,

<body onload="setTimeout('name_of_my_script.js(5)', time in milliseconds)">

Thanks guys for your help, and sorry once again about my title.

Bev

Philip M
12-23-2009, 06:37 PM
You cannnot delay the loading of your script - only its activation.

So you must include in the <head>

<script type = "text/javascript" src = "myScript.js"></script>
which will cause the script to load.

myScript.js will include a function named, for example, myfunc().

Normally you would probably have called this to activate immediately with

<body onload = "myfunc()">

But you want to delay its activation for five seconds, so

<body onload="setTimeout('myfunc()', 5000)">

godofreality
12-23-2009, 06:40 PM
no u would have to put the function u want to call after 5 seconds in that your javascript has to load when the page loads it loads as soon as it is encountered on the page if u r unsure about how to use function it basically works like this



function MyFunction(){ // this defines the function name
//this is the code u want to run
//in this area u can place as much code as u want
}//close the function


for more reading on this http://www.w3schools.com/js/js_functions.asp

another function example


function do_stuff(stuff_1, stuff_2){
alert(stuff_1+" "+stuff_2);
}

do_stuff("Hello", "World");


go ahead and mess around with the functions a bit and u will start to see how they work once u have done this using setTimeout() will make sense to u since setTimeout is a function on its own that triggers the defined function after the set time has passed which is why i had said to do



<body onload="setTimeout('myfunc()', 5000)">


what that does is after 5 seconds it triggers the function with the name myfunc which will cause all the code within the function to work

bevjones
12-23-2009, 07:43 PM
thanks for the replys guys, but where do i put

<body onload="setTimeout('myscript.js()', 5000)">

Philip M
12-23-2009, 07:55 PM
thanks for the replys guys, but where do i put

<body onload="setTimeout('myscript.js()', 5000)">

Oh dear! Please read post #5 once again.

Your page has <head> and <body> sections.

So after </head> (i.e. end of the head section)

the next line is <body> (i.e start of the body section)
and you should make this read

<body onload="setTimeout('myfunc()', 5000)">

where myfunc() is the function which is contained within the file myscript.js.
One more time - you cannot delay the loading of a script, only the activation of it.

If that is not clear I can only suggest that you give up Javascript and take up underwater motorcycling instead. :D:D

bevjones
12-23-2009, 08:12 PM
Ok I understand that, and have implemented it into my page like this:-

<HEAD>

<TITLE></TITLE>

<link rel="stylesheet" type="text/css" href="myscript.css" />
<script type="text/javascript" src="myscript.js"></script>

</HEAD>

<body onload="setTimeout('myscript.js', 5000)">

</BODY>

</HTML>

But Philip I am still getting the script loading as soon as the webpage finishes loading, not finish loading then count the 5 second timeout.

Philip M
12-23-2009, 08:16 PM
Ok I understand that, and have implemented it into my page like this:-

<HEAD>

<TITLE></TITLE>

<link rel="stylesheet" type="text/css" href="myscript.css" />
<script type="text/javascript" src="myscript.js"></script>

</HEAD>

<body onload="setTimeout('myscript.js', 5000)">

</BODY>

</HTML>

But Philip I am still getting the script loading as soon as the webpage finishes loading, not finish loading then count the 5 second timeout.


Oh dear, oh dear! You seem to be quite unable to grasp what you have been told.

One more time:-

It is NOT <body onload="setTimeout('myscript.js', 5000)">

It is

<body onload="setTimeout('myfunc()', 5000)">

where myfunc() is the function which is contained within the file myscript.js.

One more time - you cannot delay the loading of a script, only the activation of it.

bevjones
12-23-2009, 08:50 PM
Ok I get it lol sorry.

Philip would it be possible to talk to you on messenger, as I can talk to you in real time.

Philip M
12-23-2009, 08:57 PM
Ok I get it lol sorry.

Philip would it be possible to talk to you on messenger, as I can talk to you in real time.

No!

What is this script myscript.js - is it something you have downloaded from the internet? I take it that it is not something that you have created yourself.

bevjones
12-23-2009, 09:26 PM
it is something i have downloaded.

Philip M
12-23-2009, 09:40 PM
bev - it is obvious that you have virtually no knowledge or understanding of scripting. Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked. As things stand we might as well be conversing in Klingon. I suggest you try and read up the basics of Javascript and come back again in a few weeks when it will perhaps make more sense to you.

Sorry - but there is a limit to how much time I can sensibly devote to any one topic on this forum. If you cannot follow the suggestions given then it is hard to see how we can go much further.

bevjones
12-23-2009, 09:44 PM
We all have to start somewhere, by the sounds of it, you know far too much to be talking to mere mortals like myself, but thanks anyway.

Philip M
12-24-2009, 10:13 AM
We all have to start somewhere, by the sounds of it, you know far too much to be talking to mere mortals like myself, but thanks anyway.

No, I gave you simple and clear instructions, but you are unable to understand/implement them. :(

You have never given any indication of what this script you have acquired is supposed to do. If the external .js file is not too big you could post it here and then it may be posssible to help you further.

bevjones
12-24-2009, 02:33 PM
No problem, I found someone who is not too full of self importance to help me.

Thanks