I want to delay a link for a period of 500 with javascript
25,833
Solution 1
Set your href
attribute as href="javascript:delay('URL')"
and JavaScript:
function delay (URL) {
setTimeout( function() { window.location = URL }, 500 );
}
Solution 2
If you want to delay every link on your page, you can do it with jQuery like this
$(function(){
$("a").click(function(evt){
var link = $(this).attr("href");
setTimeout(function() {
window.location.href = link;
}, 500);
});
});
Author by
ammonhra
Updated on July 16, 2022Comments
-
ammonhra almost 2 years
I've been looking through the Stackoverflow questions, trying to get help with a simple link delay; I want to put it around a div, and I can't make heads or tails of the examples I've found.
So far, I understand that I need to halt the native function of href, but I don't know how to do that. The code is still very alien to me. Help?
-
VisioN over 11 yearsWhat do you mean by "delay a link"?
-
mxgr over 11 yearsDo you mean a time delay that occurs when the user clicks on a link before the browser navigates to the linked page?
-
Riju Mahna over 11 yearsPlease be more specific in the question. Most of it doesn't make much sense. Some example or snippet will be helpful
-
ammonhra over 11 yearsYes, mxgr, that's exactly what I mean.
-
-
ammonhra over 11 yearsThis is going to be a really dumb question, but where do I put this so that it affects a div?
-
gurvinder372 over 11 yearsOhh, in case this is for a div, then make it <div onclick="delay ('www.google.com')">Click here to go to google in half a second</div>
-
ammonhra over 11 yearsOh wait, I just realized what to do. Thanks.
-
Rune Vejen Petersen over 8 yearsFor this to work, you will need add evt.preventDefault(); in the beginning of your function.
-
user2840467 about 8 yearsThis works great for me - quick question: Should the () in setTimeout function() have the URL as a parameter too though?
-
Berry M. over 7 yearsThis does not support
target="_blank"
, or any othertarget
setting. -
lolcatzftw about 2 yearsThis does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review