Is it possible to make content load instantly from AJAX?
For example, on load I use $.getJSON
:
$(function(){
$.getJSON('posts.php',function(data){
data.posts.forEach(function(post){
// load info, append divs
})
})
})
There is however a slight delay (maybe 1 second) where the divs
aren't appended on page load. Is this only possible if you use PHP on-load
?
The speed of an Ajax call is the speed of an Ajax call. It's a roundtrip to the server so once you've made that roundtrip as fast as it can be on your server, there's nothing else you can do as the rest is just the speed of the transport between your server and the client browser. So, there will always be some sort of delay.
There are some things you can do as a work-around:
You can send the ajax call immediately in the
<head>
tag rather than waiting for the page to load. When the result comes back, you just store the result. Then, when the page is done loading, you instantly insert the result.You can hide your main content, run the ajax call, insert the ajax result, then show your content. This will make it all "appear" on screen at once.
The first option would look like this:
This starts the ajax call immediately as the page starts to load to get it started as soon as possible. Then, when both the ajax call is done and the DOM is ready, you can insert the content.