Improving the loading algorithm in ajax