/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How do I delay the initial exec. of a function

Hi everyone,
I know you can use setTimeOut to delay the recursive output on a function, but how do you delay the initial execution of the function. Here’s the specific
problem: I want a header to slide across the window as if it were being typed on a typewriter. That part is working! But I want to delay this effect by
3-4 seconds so it doesn’t start “typing” until after a delay.

I thought I might be able to do this by putting a setTimeOut on another function that calls the “typing” effect function. No luck. Here’s my code (it’s
quite short)

<script type=”text/JavaScript”>
window.onload=update;

function update(){
timer=setTimeOut(“update2()”, 3000);
}

var mess=”Hello there!! What do you know? It worked!!”;
var j=0;
function update2()
{
document.getElementById(“letters”).innerHTML=mess.substr(0,++j);
if(j<mess.length)
setTimeout(update2, 100);
}

Thanks in advance.
Sig

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameMay 06.2009 — A couple things, the word "javascript" should be all lowercase in the type definition in the tag. Also, setTimeout is supposed to be lowercase on the 'o', you have the 'o' uppercase in the update function. The update function is really quite unnecessary also, you could just do:
window.onload = function () {
setTimeout(function () { update2(); }, 3000);
};

Also note the use of the anonymous function within the setTimeout above. It is better to do this than quoting the function, as quoting the function as a string actually slows down the execution somewhat, as the string compiler has to start up to evaluate the string that was quoted.
Copy linkTweet thisAlerts:
@felgallMay 06.2009 — 
window.onload = function () {
setTimeout(function () { update2(); }, 3000);
};
[/QUOTE]


If there are no parameters to pass to update2 then you can simplify it further to:

window.onload = function () {
setTimeout(update2, 3000);
};


That just substitutes the named function call for the anonymous one that was calling the named function as its only action.
Copy linkTweet thisAlerts:
@captsigauthorMay 06.2009 — Many thanks for your help; the code worked! I never would have thought of using an anonymous function that specifies another anonymous function! I envy your acumen.
×

Success!

Help @captsig spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...