Solved

How to load big data step by step using ajax in jquery

Posted on 2014-02-05
2
1,094 Views
Last Modified: 2016-03-23
Hello,


I will try to explain my prolblem:

In my website I load some big data for 3 canvas.
If I load the three, it is long and I need firstly only one.

So my first solution I did isn't good.

$.when (
		$.ajax({url:"load.php"})
		.done ( function (data)
		{
		responsive_INTRODUCTION();
		}),		
		$.getJSON(url0, function (data) {
	   str_r=data.paragraphes[0].split("-");
	   str_g=data.paragraphes[1].split("-");
	   str_b=data.paragraphes[2].split("-");

		})		
		,		
		$.getJSON(url1, function (data) {
	   str1_r=data.paragraphes[0].split("-");
	   str1_g=data.paragraphes[1].split("-");
	   str1_b=data.paragraphes[2].split("-");

		})
		,		
		$.getJSON(url2, function (data) {
	   str2_r=data.paragraphes[0].split("-");
	   str2_g=data.paragraphes[1].split("-");
	   str2_b=data.paragraphes[2].split("-");

		})  
		).always (function()     
			{


		for (var i=0;i<str_r.length;i++)
			{
			pixout2[i*4  ] = str_r[i];
			pixout2[i*4 +1 ] = str_g[i];
			pixout2[i*4 +2 ] = str_b[i];
			pixout2[i*4 +3 ] = 255;
			}
		
		contextout2.putImageData(imgdout2, 0, 0);
	
		animate_BRANDING();
		
		});

Open in new window



....

That is why my idea is to do that loading only the first:
$.when (
		$.ajax({url:"load.php"})
		.done ( function (data)
		{
		responsive_INTRODUCTION();
		}),		
		$.getJSON(url0, function (data) {
	   str_r=data.paragraphes[0].split("-");
	   str_g=data.paragraphes[1].split("-");
	   str_b=data.paragraphes[2].split("-");

		})		 
		).always (function()     
			{


		for (var i=0;i<str_r.length;i++)
			{
			pixout2[i*4  ] = str_r[i];
			pixout2[i*4 +1 ] = str_g[i];
			pixout2[i*4 +2 ] = str_b[i];
			pixout2[i*4 +3 ] = 255;
			}
		
		contextout2.putImageData(imgdout2, 0, 0);
	
		animate_BRANDING();
		
		});

Open in new window





and

decomposing the loading in another place

$(document).ready( function () {


	var time_delta=500;		
	var cpt=0;	
	var int1=setInterval( function() {
		cpt++;
		if (cpt==1)
			{
			$.getJSON(url1, function (data) {
			   str1_r=data.paragraphes[0].split("-");
			   str1_g=data.paragraphes[1].split("-");
			   str1_b=data.paragraphes[2].split("-");
			   run_str1=1;
			   
				});
			}
		else if (cpt==2)
			{
			$.getJSON(url2, function (data) {
			   str2_r=data.paragraphes[0].split("-");
			   str2_g=data.paragraphes[1].split("-");
			   str2_b=data.paragraphes[2].split("-");
			   run_str2=1;
			   
				});	
			}
		else {clearInterval(int1);}
	},time_delta);

Open in new window



But it is still very long.

I believed that an ajax being executed on the server
this would not interact with the creation of the DOM on fly.

My question is the following:

How to load data step by step when they are needed
without stopping the display of all the process before any data are loaded?

Thanks David
0
Comment
Question by:DavidInLove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39838626
check this :
$(document).ready( function () {

	$.ajax({
		url:"load.php",
		success: function() {
			responsive_INTRODUCTION();
			mycall();
		}
	});

	var n = 0;
	var urls = [url0, url1, url2];

	var mycall = function() {
		$.getJSON(urls[n], mycallback);
	};

	var mycallback = function(data) {
		var v = !!n ? "str" : "str"+i;
		window[v+"_r"] = data.paragraphes[0].split("-");
		window[v+"_g"] = data.paragraphes[1].split("-");
		window[v+"_b"] = data.paragraphes[2].split("-");
		window["run_"+v] = 1;
		if(i===0) {
			for(var i=0;i<str_r.length;i++) {
				pixout2[i*4  ] = str_r[i];
				pixout2[i*4 +1 ] = str_g[i];
				pixout2[i*4 +2 ] = str_b[i];
				pixout2[i*4 +3 ] = 255;
			}		
			contextout2.putImageData(imgdout2, 0, 0);	
			animate_BRANDING();
		}
		if(++n<=2) {
			mycall();
		}
	};

});

Open in new window

0
 

Author Closing Comment

by:DavidInLove
ID: 39839952
Hi!
Yes of course callback functions are aften the solution!
Thanks really a lot for this beautifull solution I have adaptate to
my JS.

See you David
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question