admin管理员组

文章数量:1328037

I'm doing iteration, and in code last code in the iteration i put a code that will reload the page.

How to keep the JavaScript iteration running after reload and continue the state ?

E.g

for(i=0;i<5;i++) {
    document.reload();
}

After running above code, the code stopped and the console reloaded .

The code above might not work, but i wish it explain what i'm trying to do .

I'm doing iteration, and in code last code in the iteration i put a code that will reload the page.

How to keep the JavaScript iteration running after reload and continue the state ?

E.g

for(i=0;i<5;i++) {
    document.reload();
}

After running above code, the code stopped and the console reloaded .

The code above might not work, but i wish it explain what i'm trying to do .

Share Improve this question edited May 15, 2013 at 15:07 Ian 50.9k13 gold badges103 silver badges111 bronze badges asked May 15, 2013 at 15:02 batikbatik 311 silver badge2 bronze badges 5
  • When the page reloads, all the JavaScript is reloaded. Meaning all variables are deleted. You can maybe save your state in a cookie (or LocalStorage). – gen_Eric Commented May 15, 2013 at 15:04
  • @karthikr There's several options, don't be so restrictive – Ian Commented May 15, 2013 at 15:05
  • @Ian and the options are ? – karthikr Commented May 15, 2013 at 15:05
  • 4 @karthikr sessionStorage, localStorage, cookies, server sessions, window.name, query string – Ian Commented May 15, 2013 at 15:06
  • 1 Additionally, you could install Tamermonkey and create a user script with your for loop but this might be overkill. – chackerian Commented Nov 18, 2016 at 18:02
Add a ment  | 

2 Answers 2

Reset to default 4

You need to store the state in a place where it's not reset. localStorage is the ideal solution.

var i = parseInt(localStorage['i'])||0;
localStorage['i'] = i+1;
// do some stuff
// and reload if you want

You can use TamperMonkey to acplish this. TamperMonkey scripts are also reset when reloading the page. However, because they have the ability to store data and are run every time the page loads, you can implement behavior that persists across page reloads. Here is an example:

// ==UserScript==
// @name         HttpBin reload
// @namespace    http://tampermonkey/
// @version      0.1
// @description  Reload something a couple of times
// @author       Sjoerd
// @match        https://httpbin/get
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    var i = GM_getValue('counter', 0);
    GM_setValue('counter', i + 1);

    if (i < 5) {
        location.reload();
    }
})();

The GM_getValue and GM_setValue functions store data in some Tampermonkey data store, so that it is available the next time the script runs. Note that you need to use the @grant header to use those functions.

We get the value of the counter and increase it by one. Then we reload the page if the counter is less than 5. This reloads the page five times, just like in your example.

After you have run this once, the counter value in the Tampermonkey data store is set to 6 and the script won't reload anymore. If you want to reload the page another five times you will have to program something to reset the counter.

本文标签: Keep the Javascript Iteration after reload in chrome consoleStack Overflow