Problem with location.reload - Stack Overflow
给我你的怀抱
给我你的怀抱 2017-05-18 10:51:54
0
3
1225
start.onclick=function newGame(){
    location.reload();
    flag = selectModel[0];
    console.log(flag);
};

I originally wanted to refresh the page and assign the global variable flag after clicking. Successfully refreshed, but failed to assign value successfully; if location.reload is commented out, value can be assigned.

Is location.reload() asynchronous? When the value is assigned, the refresh has not yet been executed, but the refresh operation is performed after the assignment is completed. Is this true?

给我你的怀抱
给我你的怀抱

reply all(3)
習慣沉默

You first need to understand the life cycle of javascript in the browser.

After reloading the page and entering the next life cycle, the flag assigned in the previous cycle will be destroyed even if the assignment is successful.

Refreshing the page is equivalent to throwing everything away and starting over, just like reopening the page.

What weird requirement would require you to modify the global flag after clicking a link to refresh?

刘奇

You can save this flag to localdtorage

我想大声告诉你

It should be synchronized, so a better way is to consider this:

start.onclick=function newGame(){
    flag = selectModel[0];
    console.log(flag);
    setTimeout(() => {
        location.reload();
    });
};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template