Ajax jQuery
Ajax jQuery

Reputation: 345

Window.location.href infinite loop

When using window.location.href I'm running into an infinite loop (even though it is placed inside a function that is only called once during startup).

function onYouTubeIframeAPIReady() { // only called one time once API's are ready
        window.location.href = ("?name=" + new Date().getTime()); //is EPOCH time

window.location.hash works just fine (but I can't use that) ...

Upvotes: 2

Views: 10695

Answers (2)

Scott
Scott

Reputation: 3761

You are creating your own loop.

On startup of the page you are calling:

window.location.href = ("?name=" + new Date().getTime());

Which causes the page to load itself with a new ?name=time appended on the end.

What you may want to do instead is change the hash part of the URL. Like so:

window.document.location.hash = new Date().getTime();

Otherwise you should conditionally call window.location.href so that it only executes at certain times, like so:

function onYouTubeIframeAPIReady() { // only called one time once API's are ready
    if (someVariable == "refreshNow") {
          window.location.href = ("?name=" + new Date().getTime()); //is EPOCH time
    }
}

Upvotes: 5

rfornal
rfornal

Reputation: 5122

If this function is run during startup and there is nothing conditional to stop it from being run, the window.location.href portion will execute each time, causing the looping you describe. You need to provide some condition that will stop the loop.

Upvotes: 0

Related Questions