/    Sign up×
Community /Pin to ProfileBookmark

Triggering setTimeout

Hi,
I have a little problem with setTimeout.
Consider the following code:

var alertTimerId = 0;
alertTimerId = setTimeout ( “showAlert(someParameter);”, 3000 );

Now let’s say that I need to trigger the function I timed-out before the designated time.
I can’t just use:

clearTimeout(alertTimerId);
showAlert(someParameter);

Because at this point I don’t know what’s the value of someParameter.
(Yes, I can store it in a global var… blah blah blah… I’m looking for a smart solution…)

Is there something like runTimeout(alertTimerId)?

Thanks,
Z

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 21.2008 — try:
<i>
</i>if(alertTimerId){
clearTimeout(alertTimerId)
}

But it is not clear for me why you don't yet know the value of the parameter. can you provide a real simplified example?
Copy linkTweet thisAlerts:
@z0333authorJul 21.2008 — I have a scenario in witch I set a timeout in one of the following manors:

setTimeout ( "showAlert(x);", 3000 );

or

setTimeout ( "showAlert(y);", 3000 );

or

setTimeout ( "showAlert(z);", 3000 );

Now let's say that if a user clicks a button before the timeout elapses, I need to execute the timed-out command *NOW* (with the appropriate parameter) and cancel the timeout.

Since the "full" command (the function call with the right parameter) is "stored" somewhere waiting for execution, I though it would be nice just to call it, instead of storing the parameter, clearing the time out, and calling the function with the stored parameter&#8230;

Is there a way to execute the function "stored" in the timeout before it's time?
Copy linkTweet thisAlerts:
@Declan1991Jul 21.2008 — No.

Also, you should pass a function to setTimeout instead of a string. It's much more flexible because it doesn't run in global scope.
×

Success!

Help @z0333 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,
)...