1

I'm wondering if this javascript:

function twoPlusTwo() {
   var four = 2 + 2;
   $.post(document.URL, {'answer' : four}) //with a page refresh
   showNumber(four);
}

function showNumber(four){ 
   $('#no_refresh').html(four);
} 

twoPlusTwo();

and this html/swig,

<div id="refresh">{{post.answer}}</div>
<div id="no_refresh"></div>

would render faster if I pass the parameter and make a single (not round) trip to the server? My intuition says that it would, but that's been wrong before. If yes, than in a scenario where someone is driving through a tunnel while accessing a heavy page on the mobile version of your website, will it cause a significant difference?

user229044
  • 232,980
  • 40
  • 330
  • 338
Squirrl
  • 4,909
  • 9
  • 47
  • 85
  • 1
    You're pretty much describing the reason AJAX exists... – user229044 Dec 21 '13 at 09:29
  • 1
    Depending on how much other HTML, etc. is on the page, then yes, avoiding a round trip will avoid rebuilding the DOM from scratch unnecessarily and also eliminate network overhead. As @meagar pointed out, this is basically why we use AJAX... – Jesse Dec 21 '13 at 10:03
  • Thanks a lot. I may be wrong, but it looks to me like AJAX does communicate with server before updating the client. http://www.w3schools.com/ajax/ajax_intro.asp | http://stackoverflow.com/questions/6009206/what-is-ajax-and-how-does-it-work which is what makes it cool. I understand that with AJAX, JS will run until the json get's back but isn't it still contingent on a connection, which if severed, would mean no update? Would passing a parameter (while a connection for a one-way post is sought and delivered) be faster and more dependable? – Squirrl Dec 21 '13 at 10:57

0 Answers0