JQuery Find and Replace - This is a Hanging Browser! Is the data size too large?

With a lot of help from @kalley, we found that if I comment on the next two lines, the LAG is gone!

var $tableContents = $table.find('tbody') var $html = $('<tbody/>').html(data); 

But how do I save the above, but cancel the LAG?


FURTHER INFORMATION: The code below works, but the problem is that $.GET causes the browser to freeze until the ajax request is completed. Do I need (flow control?) Or something that will solve this problem without blocking / freezing the browser until ajax completes the GET request.

The largest LAG / Lockup / Hang is in $.get("updatetable.php" , since the others only return 7 or less (number) of values, and this (updatetable.php) returns more (200-300kb). I would like implement some kind of flow control here or make the script wait like 5 seconds before running the update command for tablesort and before displaying the toast message so that ajax can get $.get("updatetable.php" , which I just don’t do, Understand why Does it block the browser when it receives data? Does it try to run other commands and what causes the LAG?

Below are the STEPS

1. $.get("getlastupdate.php" Will fire every 10 seconds or so to check if the date and time match the return data: 20130812092636 format: YYYmmddHHmmss.

2. if the date and time do not coincide with the last GET, then $.get("getlastupdate2.php" will work, and this data will be sent back and put in the toast message and sent to the user $().toastmessage('showNoticeToast', Vinfoo );

3. before or after the above ( $.get("getlastupdate2.php" ) another GET is launched: $.get('updatetable.php' this will GET updated information about the table. And replace the old with new information. And then update / apply table

4. at the end of all this, I want $.get("ajaxcontrol.php" , and this will return 1 or 2 if the user logs in, then he will be 2, and this is 1, and he will destroy the session and log out.

  <script type="text/javascript" src="tablesorter/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="tablesorter/final/jquery.tablesorter.js"></script> <script type="text/javascript" src="tablesorter/final/jquery.tablesorter.widgets.js"></script> <script type="text/javascript" src="tablesorter/final/toastmessage/jquery.toastmessage-min.js"></script> <script type="text/javascript" src="tablesorter/qtip/jquery.qtip.min.js"></script> <script type="text/javascript"> var comper; function checkSession() { return $.get("ajaxcontrol.php", function (DblIn) { console.log('checking for session'); if (DblIn == 1) { window.location = 'loggedout.php'; } }).then(updateTable); } function checkComper() { var SvInfo; var onResponse = function (comperNow) { if (comper === undefined) { comper = comperNow; } else if (comper !== comperNow) { var Vinfoo; comper = comperNow; // returning this $.get will make delay done until this is done. return $.get("getlastupdate2.php", function (primaryAddType) { Vinfoo = primaryAddType; $().toastmessage('showNoticeToast', Vinfoo); }).then(checkSession); } }; $.get('getlastupdate.php').then(onResponse).done(function () { tid = setTimeout(checkComper, 2000); }); } function updateTable() { return $.get('updatetable.php', function (data) { console.log('update table'); var $table = $("table.tablesorter"); var $tableContents = $table.find('tbody') var $html = $('<tbody/>').html(data); $tableContents.replaceWith('<tbody>' + data + '</tbody>') //$tableContents.replaceWith($html) $table.trigger("update", [true]); var currentUrl = document.getElementById("frmcontent").contentWindow.location.href; var urls = ['indexTOM.php', 'index1.php'], frame = document.getElementById('frmcontent').contentDocument; for (var i = 0; i < urls.length; i++) { var url = urls[i]; if (frame.location.href.indexOf(url) !== -1) { frame.location.reload() } } $('[title!=""]').qtip({}); }); }; $(function () { var tid = setTimeout(checkComper, 2000); $("#append").click(function (e) { // We will assume this is a user action e.preventDefault(); updateTable(); }); // call the tablesorter plugin $("table.tablesorter").tablesorter({ theme: 'blue', // hidden filter input/selects will resize the columns, so try to minimize the change widthFixed: true, // initialize zebra striping and filter widgets widgets: ["saveSort", "zebra", "filter"], headers: { 8: { sorter: false, filter: false } }, widgetOptions: { filter_childRows: false, filter_columnFilters: true, filter_cssFilter: 'tablesorter-filter', filter_filteredRow: 'filtered', filter_formatter: null, filter_functions: null, filter_hideFilters: false, // true, (see note in the options section above) filter_ignoreCase: true, filter_liveSearch: true, filter_reset: 'button.reset', filter_searchDelay: 300, filter_serversideFiltering: false, filter_startsWith: false, filter_useParsedData: false } }); // External search $('button.search').click(function () { var filters = [], col = $(this).data('filter-column'), // zero-based index txt = $(this).data('filter-text'); // text to add to filter filters[col] = txt; $.tablesorter.setFilters($('table.hasFilters'), filters, true); // new v2.9 return false; }); }); </script> 
+4
source share
2 answers

Perhaps instead of using setInterval you should switch to setTimeout . This will give you more control when time repeats:

 function checkComper() { var SvInfo; var onResponse = function (comperNow) { if (comper === undefined) { comper = comperNow; } else if (comper !== comperNow) { var Vinfoo; comper = comperNow; // returning this $.get will make delay done until this is done. return $.get("getlastupdate2.php", function (primaryAddType) { Vinfoo = primaryAddType; $().toastmessage('showNoticeToast', Vinfoo); }).then(checkSession); } }; $.get('getlastupdate.php').then(onResponse).done(function () { tid = setTimeout(checkComper, 10000); }); } var tid = setTimeout(checkComper, 10000); 

Then you can save it async: true

Here's a fiddle showing that it works using echo.jsontest.com and some fudging numbers.

Since the click event callback seems to be where the problem is, try doing this and see if it removes the delay (I deleted other comments to make it more concise):

 function checkSession() { return $.get("ajaxcontrol.php", function (DblIn) { console.log('checking for session'); if (DblIn == 1) { window.location = 'loggedout.php'; } }).then(updateTable); } function updateTable() { return $.get('updatetable.php', function (data) { console.log('update table'); var $tableContents = $table.find('tbody') //var $html = $('<tbody/>').html(data); //$tableContents.replaceWith($html); // replaceWith text seems to be much faster: // http://jsperf.com/jquery-html-vs-replacewith/4 $tableContents.replaceWith('<tbody'> + data + '</tbody>'); //$table.trigger("update", [true]); var currentUrl = document.getElementById("frmcontent").contentWindow.location.href; var urls = ['indexTOM.php', 'index1.php'], frame = document.getElementById('frmcontent').contentDocument; for (var i = 0; i < urls.length; i++) { var url = urls[i]; if (frame.location.href.indexOf(url) !== -1) { frame.location.reload() } } $('[title!=""]').qtip({}); }); }; $("#append").click(function (e) { // We will assume this is a user action e.preventDefault(); updateTable(); }); 

I commented on $table.trigger("update", [true]) , since if you sort the table on the server before returning it, you will not need to run it, which I'm pretty sure where the bottleneck is.

+2
source

It is very difficult to unravel the mess that you have, but if you want ajax requests every 10 seconds it makes sense to separate this logic from the business logic from the data from the server.

Your code will also really benefit from using promises. Consider this example

  $(document).ready(function() { var myData = { } , ajaxPromise = null setInterval(callServer, 1000) function callServer() { ajaxPromise = updateCall() .then(controlCall) .done(handler) .error(errorHandler) } function updateCall() { return $.get('updateTable.php', function(data) { myData.update = data }) } function controlCall( ) { return $.get('ajaxControl.php', function(data) { myData.control = data }) } function handler() { console.dir(myData) } function errorHandler(err) { console.log(err) console.dir(myData) } }) 
+1
source

Source: https://habr.com/ru/post/1496176/


All Articles