Run
Run

Reputation: 57286

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.

Upvotes: 2

Views: 9754

Answers (2)

AndreKR
AndreKR

Reputation: 33697

The problem here is not the delay. $(window.location).attr('href','http://stackoverflow.com/') wouldn't work either, because href simply isn't an attribute of window.location because window.location isn't a DOM node at all.

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630627

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.

Upvotes: 8

Related Questions