/    Sign up×
Community /Pin to ProfileBookmark

How do i place a 2 second wait before a function exits?

I need to place a 2 second wait on the function listed
below. When a user clicks on a link, i need to call the
target.src = ‘/Test.aspx?referrer=’ + document.referrer;
and then wait for 2 seconds before exiting the function.

anchorList[i].onclick = function () {
var href = this.getAttribute(“href”);
if (href != ”) {
var target = new Image();
target.src = ‘/Test.aspx?referrer=’ + document.referrer;
//Wait for 2 seconds.
}
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayNov 07.2006 — This should be close:
<i>
</i> var stDate = new Date();
var eDate = new Date();
while ((eDate - stDate) &lt; 2000){
eDate = new Date();
}


Note that the screen will not refresh while this is running though.
Copy linkTweet thisAlerts:
@kingpoopauthorNov 09.2006 — Thanks for the tip. That works great. Here's how I ended up doing it.

function nap(m) {

var snooze = new Date(new Date().getTime() + m);

while (new Date() < snooze) {

//napping

}

}

<script>nap(5000);</script>
×

Success!

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