0

I am very new to Javascript and I am having difficulty figuring out how to stop it after running first time. I have a simple refresh function and I want it to run once onLoad. Now it keep running. I would appreciate it if you can tell me how to stop it after the first run. Thank you.

<script>
function reload(){
   location.reload();
}
</script>
aka
  • 1
  • 3

2 Answers2

0

By writing this code, you create an infinite loop:

1/First you access the page

2/then the js code is loaded, and refresh the page

3/then the js code is loaded again, and refresh the page.

4/then .......

So the page 'keep running'

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
0

I dont know how you set refresh process but you can do something like this.

Say you got a query string and if it's set to something you wanna refresh the page e.g. URL?refresh=1

You can read this refresh value and check if it 's set to 1 . If it is redirect the page

function getParameterByName(name) {
  var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

refresh = getParameterByName("refresh");

if (refresh == "1") window.location = "URL";

FIDDLE

PS: getParameterByName function is found in HERE

Community
  • 1
  • 1
Batu.Khan
  • 3,060
  • 2
  • 19
  • 26