setInterval breaks on window.locaion / window.open

341 views Asked by At

I'm doing some code that requires actions on multiple sites (get some data switch to another site etc.) in a loop.

I'm trying to do this using setInterval().

Simplified, the task looks like this when launched in a console:

function checkit() { 
    window.location='http://www.google.pl'; 
} 
var nre = setInterval(checkit,5000);

I have tried launching this script (in more complicated forms through different measures, from bookmarklet, from server side script etc, the interval runs OK in my original code, even does everything what I require in a loop until another page is called (through window.open or window.location). than the loop just seizes to execute.

I'm pretty new to JS (2 days experience) so I'm probbably doing something uterly stupid. Any Advice on how to get this thing going (is this even possible)?

Best regards

1

There are 1 answers

1
Nemesis02 On BEST ANSWER

The problem you're going to have is that JS doesn't stay from page to page, so once the page changes, that loop goes away. You'll need to have the JS on each page you're wanting to visit to continue flow and even then, the variables are nuked when you change pages.

The only way to circumvent this issue is by storing a serialized object (or JSON string) within the window.name value which is remember across pages and domains within that tab.