Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: delay() + window.location?

I know that we can delay the url redirection easily with plain javascript below,

setTimeout(function(){ document.location = 'http://stackoverflow.com/';}, 2000 ); 

what if I want to use jQuery's delay()?

$(window.location).delay(4000).attr('href', 'http://stackoverflow.com/');// fail to work!

Any ideas?

thanks.

like image 415
Run Avatar asked Apr 18 '26 02:04

Run


1 Answers

Simple answer: don't use .delay() or attempt to, it's a round-about really non-sensical way to get what you're after, since it's just calling setTimeout() underneath.

It wasn't designed for this at all (it's for queuing, and then primarily for animations), and you're trying to use the location in a $() wrapper (which is trying to use it as a selector), which is also wrong.

Use jQuery (or any other abstraction layer, in any language) when it makes sense to do so, it definitely makes no sense here, use setTimeout(), save yourself the confusion and the client the CPU cost.

like image 198
Nick Craver Avatar answered Apr 20 '26 13:04

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!