NameSilo

Simple Javascript delay help needed

Spaceship Spaceship
Watch

youngempire

Established Member
Impact
3
I need some quick help to setup a delay on my javascript code below so that there will be a 15 second delay before the javascript code executes.

Code:
<script type="text/javascript" src="example.js">
</script>

I tried the setTimeout function but could not get it to work. Any help is greatly appreciated! :D
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
your example.js must be in the same dir with file include
 
0
•••
0
•••
0
•••
setTimeout("function expression",delaytime)

how do I setup the src="example.js" so that it is a function?

i.e.
Code:
function fetch(){
"example.js"
setTimeout("fetch",2000);
}

Any ideas?
 
0
•••
you would have to define a function that either contains all of the functions of example.js (or find the name of the one function found in example.js) and call that.
 
0
•••
you would have to define a function that either contains all of the functions of example.js (or find the name of the one function found in example.js) and call that.

Right this is what I don't know how to do and am seeking help with.
 
0
•••
ah ok, in that case, it woudl depend on how your example.js file is laid out. do you have a copy/example of it? (lol)
 
0
•••
ah ok, in that case, it woudl depend on how your example.js file is laid out. do you have a copy/example of it? (lol)

Sorry I don't. But the original code above with the src="example.js" works fine, I just need help putting a delay on it.
 
0
•••
ok well the way that javascript works is that on some event, it runs some code. this code that it runs can be defined as a function (and therefore given a name). i honestly cant help you to get the exact code without seeing example.js, as it could have a multitude of things.

like if it were just:

Code:
function popup() {
alert("Hi!");
}

then, the function name in setTimeout would be "popup". However, most javascript scripts are much more complex and call a variety of functions, so you'd probably have to remove whatever trigger is making it run (it could be a onLoad in the body html tag, for example), consilidate it so it can be run by one function call, and then use that for the setTimeout.

if you don't want to post the example.js file here, i'm open to PMs
 
0
•••
Put everything you want to do in a function, then call the function in the setTimeout function.

Try put the following in your page, and the alert will pop up 3 seconds after the page is loaded.

<script>

function popup() {

alert('Hi!');

}

setTimeout('popup()',3000);

</script>
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back