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?
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: