javascript - Non-blocking Synchronous AJAX -
is there way perform synchronous ajax query doesn't freeze browser? in opinion synchronous requests lot easier work in cases, fact block other parts of code executing real killer. there way synchronous ajax without negative side effects? (and yes, realize term "synchronous ajax" oxymoron.)
i'll provide example of bad side of effects of allowing such behavior.
lets have program:
<script> var file = "foo.json"; function nullit() { file = null; } function loadfile() { if (file != null) { synchronousload(file);//imagine load takes 5 seconds alert("i loaded: " + file); } } window.onload = loadfile; </script> <button onclick="nullit()">click me</button>
the bad thing here-
- while
synchronousload()
blocking 5 seconds, user clicks button, , event handler runs completion. - now
file
variable null. synchronousload()
finishes , returns, letting execution resume on next line of code- but
file
null, , message output user broken.
the real issue here cannot reason code same way anymore. because fact true on line 5, doesnt mean still true on next line. makes difficult write error free program.
some programming languages support multithreading, , have deal these issues, although have tools deal these problems. but, it's still lot of effort on programmers part.
comparatively speaking, using callbacks asynchronous operations ez-mode.
Comments
Post a Comment