There is a piece of code with ajax loading information and calling the function immediately after that. How to make so that in the case of the very first request the welcome page was called, and after clicking on the button the function with content was loaded. I do this, but for some reason every time the first if block is loaded and even when the page is updated, the count counter does not grow, but is reset.

 $(function() { var pathToSome = 'some.php'; var count = 0; $.get(pathToSome, function(data) { if (count === 0) { $('#content').html("<h1>Стартовая страница</h1><p><a href="#">Перейти</a></p>"); } else { someFunction(); console.log("Do some function"); } count++; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel-body" id="content"></div> 

  • one
    Try count for $(function() { . - Suvitruf
  • @Suvitruf tried, the effect did not give, the count is still reset - Vasya

2 answers 2

If you want the variable to remain in memory after the page is reloaded, then you need to store it somewhere, for example, in the cache or local storage. Then, after the user enters the page, the counter can be increased or start a boolean variable.

If counting is not needed when reloading the page, you can simply hide the greeting block when you click the button.

  • I do this, but it still does not work .. localStorage.setItem('myKey', 'true'); var localValue = localStorage.getItem('myKey'); $(function(){ $.get(pathToPolls, function(data) { if (localValue) { localStorage.removeItem("myKey"); localStorage.clear(); } else {...} localStorage.setItem('myKey', 'true'); var localValue = localStorage.getItem('myKey'); $(function(){ $.get(pathToPolls, function(data) { if (localValue) { localStorage.removeItem("myKey"); localStorage.clear(); } else {...} localStorage.setItem('myKey', 'true'); var localValue = localStorage.getItem('myKey'); $(function(){ $.get(pathToPolls, function(data) { if (localValue) { localStorage.removeItem("myKey"); localStorage.clear(); } else {...} }); - Vasya
  • one
    @ Vasya If I understood correctly that after the user saw the greeting when you first logged in, you need not to show it again, then when you load the page you need to do localStorage.setItem('helloIsSeen', 'false'); , then, when the request is successfully localStorage.setItem('helloIsSeen', 'true'); . In if put a check on helloIsSean - that is, the user saw the greeting. Then, if he saw, then that in else will be executed. In your code, you clear localStorage on every load, the value of the variable becomes true and it is again cleared. - petrichor

variable var count = 0; You have it so declared, and after receiving the answer you do not count it. as written above, bring it higher in code beyond the limits of the function. and it turns out that every time you start a function, you define it as equal to zero.