jquery - Alternative to JavaScript's setInterval? -


i need contents shown in real-time, when loading many things takes cpu , laggy.

is there alternative code below?

$(document).ready(function() {     var refresh_bal = setinterval(         function (){             $.get('./php/get_balance.php', function(balance) {                 $('.balance').html(balance);             });         }, 1000);      var refresh_total = setinterval(         function (){             $.get('./php/get_total_bets.php', function(total) {                 $('.total').html(total);             });         }, 1000);      var refresh_profit = setinterval(         function (){             $.get('./php/get_profit.php', function(profit) {                 $('.profit').html(profit);             });         }, 1000);      $('.haa').on('click', function() {         var refresh_bets = setinterval(             function (){                 $.get('./php/get_bets.php', function(bets) {                     $('.betstable').html(bets);                 });             }, 1000);     });      var refresh_site = setinterval(function (){         $.get('./php/get_site.php', function(site) {             $('.sitestats').html(site);         });     }, 1000);      var refresh_client = setinterval(function (){         $.get('./php/get_client.php', function(client) {             $('.clientshow').html(client);         });     }, 1000);      var refresh_server = setinterval(function (){         $.get('./php/get_server.php', function(server) {             $('.servershow').html(server);         });     }, 1000);      $('.ha').on('click', function() {         var refresh_chat = setinterval(function() {             $.get('./php/get_chat.php', function(chats) {                 $('.chats').html(chats);             });         });     }); }); 

there 2 primary things can improve performance of code without moving websockets.

first, replace setinterval settimeout when dealing recurring ajax requests. reasoning doing if you're using setinterval, next may sent before previous finishes can crash browser. using settimeout, ensure previous complete before request next.

(function refreshbalance() {     $.get('./php/get_balance.php', function(balance) {         $('.balance').html(balance);         settimeout(refreshbalance,1000);     }); })(); 

next, consolidate of requests few requests possible, ideally one. because each request make, headers , cookies have resent, , browsers have limit maximum number of concurrent http requests can sent single domain @ time. if said limit reached, ajax requests delayed until previous ones complete. can lock browser.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -