2

I use this ajax script :

$.ajaxSetup ({
        // Disable caching of AJAX responses
        cache: false
    });

    function getRandomInt() {
      return Math.floor(Math.random() * Math.pow(10,6));
}

    $(document).ready(function(){

        $('#edit_in_buy_usd').click(function(){
            $.post("edit_id.php?rnd=" +getRandomInt(), 

                   {edit_id: $('#edit_in_buy_usd').val()}, 

                   function(data){

                  $('#id').html(data);

                }
            );

        });

});

after pressing the button, I create a variable that goes to edit_id.php ,performs certain processes and returns the result to the same Div with id="id". Everything works perfectly but ... after pressing the button about 30-40 times the page starts to work super slow. I read a lot of topics I added things but I do not have much experience in Ajax tehnique.... and can not handle it alone ... If you help me I will be very grateful to you .. thank you in advance!

thesecretmaster
  • 1,950
  • 1
  • 27
  • 39
  • Why are you sending 30-40 requests? what are you trying to achieve? maybe you can reduce this to fewer requests. Also - how big is your response? Maybe you send too much data. Too much HTML will cause this problem. – HTMHell Jun 17 '18 at 15:26
  • the page is a table that is often clicked and so it is collected the request, the information that is reloaded in the div is big... but I'm sure there is some way to clear it from the browser after every request for example. – Filip Ivanov Jun 17 '18 at 15:35
  • if you control the server side too, you might checkout if it is an option to send only the updates back instead of the "big" thing – hootnot Jun 17 '18 at 15:54
  • Definitely consider what @user2413548 said. Reloading an entire data set in a responsive, repeated manner is normally a recipe for disaster. It's about sending back the changes only, not *everything*. – Mitya Jun 17 '18 at 16:27
  • You might want to consider using JSON, and build the necessary HTML with the given parameters. – HTMHell Jun 17 '18 at 18:55

2 Answers2

2

Well I know that this question was made a long time ago, but in case someone from now on is looking for some kind of answer. I just wanted to add that I read something on this site: https://www.phpclasses.org/blog/post/277-Fix-the-AJAX-Requests-that-Make-PHP-Take-Too-Long-to-Respond.html So that basically what it teaches is a way to optimize your web site in case you using PHP

In the case you're using PHP sessions, you have to use the function session_start(), so that when you call it, PHP loads the session file and serialize it to access the variables through $_SESSION, but it keeps the file blocked by the script.

So what the site suggests is that after you're done with your validations, use the session_write_close which will release the file.

The answer of why you should do that is because when you make simultaneous AJAX requests and you're using sessions to validate or whatever, the server will block the file until the script ends and then the next request will be executed.

So I would suggest to validate and release and in case that they're some data the you would like to use later, consider saving the value in a normal PHP variable rather than using the $_SESSION super array everytime.

This is just something that might increase the speed of your server responses and it's not intended to be the absolute answer to the asked question, I really hope it helps and that you find the best programming practices to speed up your site!

Noé
  • 149
  • 8
  • if you use the` [session_write_close ()](https://www.php.net/manual/es/function.session-write-close.php) ` you can still access to the `$_SESSION` super array but changes won't be made, I suggest to save it on a variable in case you use the function to prevent future **logical errors** – Noé Apr 28 '21 at 02:00
0

Instead of retrieve all of table data from server script :

$('#id').html(data);

try to append just the new row data to table like this :

$("#id").append(data);

Server side script must return a value like this : <tr><td>test</td><td>test</td></tr>

Root
  • 2,269
  • 5
  • 29
  • 58
  • in this case do not accumulate ajax request but the DIV repeat one after another .. in my case the information in DIV have to update – Filip Ivanov Jun 17 '18 at 16:47
  • use developer console in chrome,firefox to troubleshoot and debug it via `network` tab in web consoles or `memory usage` on task manager of browser (inside tool) of page that response is slowdown or have a memory leak . – Root Jun 17 '18 at 16:58
  • @FilipIvanov it may have two problem : `(1)` respone from the server is slowdown after some requests , `(2)` the webpage of a memory leak and slowdown the browser tab . – Root Jun 17 '18 at 17:03