When you reload your page, all javascript code that you have written is lost. In your code, the location.reload()
function is executed before alert()
, but I suspect because it takes time for the location.reload()
function to fully execute, it still has plenty time executing the alert()
function, and also all the other iterations aswell, which leads to the while loop to finish before the page has reloaded.
location.reload()
probably falls under the category of asynchronous functions, which means the function starts executing and keeps running in the background. All code that follows a asynchronous function is immediately executed, regardless of whether the asynchronous function has finished by then or not.
If you want to keep track of the visitor count, make use of some php and save the data in a database.
UPDATE:
Put this code into a .html file and open it. Reload it as many times as you wish, and check the currentClicks
variable in your console.
<!DOCTYPE html>
<body>
<script>
var currentClicks = localStorage.getItem("clickCount");
currentClicks++;
localStorage.setItem("clickCount", currentClicks);
</script>
</body>
</html>