4

Currently, I'm using setTimeout() to pause a for loop on a huge list so that I can add some styling to the page. For instance,

Eg: http://imdbnator.com/process?id=wtf&redirect=false

What I use setTimeOut for:

I use setTimeout() to add images,text and css progress bar (Why doesn't Progress Bar dynamically change unlike Text?2).

Clearly, as you can see it is quite painful for a user to just browse through the page and hover over a few images. It gets extremely laggy. Is there any any workaround to this?

My FOR Loop:

Each for loop makes an ajax request on the background to a PHP API. It definitely costs me some efficiency there but how do all other websites pull it off with such elegance? I mean, I've seen websites show a nice loading image with no user interference while it makes an API request. While I try to do something like that, I have set a time-out everytime.

Is that they use better Server-Client side interaction languages like the node.js that I've heard?

Also, I'e thought of a few alternatives but run into other complications. I would greatly appreciate if you can help me on each of these possible alternatives.

Method 1:

Instead of making an AJAX call to my PHP API through jQuery, I could do a complete server side script altogether. But then, the problem I run into is that I cannot make a good Client Side Page (as in my current page) which updates the progress bar and adds dynamic images after each of the item of the list is processed. Or is this possible?

Method 2: (Edited)

Like one the useful answers below, I think the biggest problem is the server API and client interaction. Websockets as suggested by him look promising to me. Will they necessarily be a better fix over a setTimeout? Is there any significant time difference in lets say I replace my current 1000 AJAX requests into a websocket?

Also, I would appreciate if there is anything other than websocket that is better off than an AJAX call.

How do professional websites get around with a fluidic server and client side interactions?

Edit 1: Please explain how professional websites (such as http://www.cleartrip.com when you are requesting for flight details) provide a smooth client side while processing the server side.

Edit 2: As @Syd suggested. That is something that I'm looking for.I think there is a lot of delay in my current client and server interaction. Websockets seem to be a fix for that. What are the other/ best ways for improving server cleint interaction apart from the standard AJAX?

Community
  • 1
  • 1
  • why AJAX? why not just create `img`? when u set `src` attribute for it, its triigger the browser to load it. and when image loaded, an event `onload` for this image will be fired up. Image now in browser cache and you can use provided `src` value for instant image *loading*. Most should run asynchonous, on events fired. – befzz Jun 17 '15 at 12:09
  • I'm using AJAX to run something on the server side and return to client side. Not for fetching images. –  Jun 17 '15 at 12:44
  • check. you are making one synchronous ajax for 1 film. its very bad. but must do 1 asynchronous for bulk of 10-100. Synchronous ajax is blocking execution of code. – befzz Jun 17 '15 at 12:56
  • Yes, I tried using a asynchronous call through jquery as far as I remember, thats why I decided to use the good old synchronous one. I tried changing to 'true' and its giving me an error. –  Jun 17 '15 at 13:09
  • 1
    update backed. it should return 10-1000 films per request. not only one. http-connection is *cost* too much. – befzz Jun 17 '15 at 13:11
  • Yes, that is one work around I suppose. But for which I'll have to convert my front end Javascript API to PHP which is a pain. For which, I was looking for a workaround. –  Jun 17 '15 at 13:13
  • Besides, async calls are not being made here for more number of reasons because of my API. So that definitely won't work though I've already thought about this. Thanks! –  Jun 17 '15 at 13:17
  • Hi ra... What's the problem are you facing with `setTimeout`? – Praveen Kumar Purushothaman Jun 25 '15 at 08:09
  • The link is dead making this question impossible top answer. Also you haven't shared any code here on site. Also this question ("how professional websites (such as http://www.cleartrip.com) provide a smooth client side while processing the server side.") is too broad to answer either way. – PeeHaa Jun 25 '15 at 08:13
  • @PraveenKumar I'm uable to think of a way to provide a good user exprience while I'm using settimeOuts in the background.. hover over the images and you'll see what I'm referring to. –  Jun 25 '15 at 08:15
  • @PeeHaa I've made a hyperlik change i my post. Check it out –  Jun 25 '15 at 08:16
  • Link still does not work. – skobaljic Jun 25 '15 at 08:24

2 Answers2

5

Your first link doesn't work for me but I'll try to explain a couple of things that might help you if I understand your overall problem.

First of all it is bad to have synchronous calls with large amount of data that require processing in your main ui thread because the user experience might suffer a lot. For reference you might want to take a look into "Is it feasible to do an AJAX request from a Web Worker?"

If I understand correctly you want to load some data on demand based on an event. Here you might want to sit back and think what is the best event for your need, it's quite different to make an ajax request every once in a while especially when you have a lot of traffic. Also you might want to check if your previous request has completed before you initialize the next one (this might not be needed in some cases though). Have a look at async.js if you want to create chained asynchronous code execution without facing the javascript "pyramid of doom" effect and messy code.

Moreover you might want to "validate - halt" the event before making the actual request. For example let's assume a user triggers a "mouseenter" you should not just fire an ajax call. Hold your breath use setTimeout and check if the user didn't fire any other "mouseenter" event for the next 250 ms this will allow your server to breath. Or in implementations that load content based on scroll. You should not fire an event if the user scrolls like a maniac. So validate the events.

Also loops and iterations, we all know that if the damn loop is too long and does heavy lifting you might experience unwanted results. So in order to overcome this you might want to look into timed loops (take a look at the snippet bellow). basically loops that break after x amount of time and continue after a while. Here are some references that helped me with a three.js project. "optimizing-three-dot-js-performance-simulating-tens-of-thousands-of-independent-moving-objects" and "Timed array processing in JavaScript"

//Copyright 2009 Nicholas C. Zakas. All rights reserved.
//MIT Licensed
function timedChunk(items, process, context, callback){
    var todo = items.concat();   //create a clone of the original

    setTimeout(function(){

        var start = +new Date();

        do {
             process.call(context, todo.shift());
        } while (todo.length > 0 && (+new Date() - start < 50));

        if (todo.length > 0){
            setTimeout(arguments.callee, 25);
        } else {
            callback(items);
        }
    }, 25);
}

cleartip.com will probably might use some of these techniques and from what I've seen what it does is get a chunk of data when you visit the page and then upon scroll it fetches other chunks as well. The trick here is to fire the request a little sooner before the user reaches the bottom of the page in order to provide a smooth experience. Regarding the left side filters they only filter out data that are already in the browser, no more requests are being made. So you fetch and you keep something like cache (in other scenarios though caching might be unwanted for live data feeds etc).

Finally If you are interested for further reading and smaller overhead in data transactions you might want to take a look into "WebSockets".

Community
  • 1
  • 1
0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
  • Thanks! I think websockets is something that I was looking for. I think there is a lot of delay in my current client and server interation. Websockets seem to be a fix for that. What are the other/ best ways for improving server cleint interaction apart from the standard AJAX? –  Jun 25 '15 at 15:34
  • @SaiKrishnaDeep well although this is another question :P at this point you got ajax and websockets. Also some libraries which handle communication between client and server more efficiently and various ways of transport search for socket.io (although it is a nodejs thingy).What I try to do in my projects (regarding server-side APIS) is to avoid multiple db interactions, cache if I can, create the data responses in the most minimalistic and efficient way possible and leave the rendering to the client. Also you could cache the responses in your client locally for a small amount of time. – 0x_Anakin Jun 26 '15 at 12:12
  • Don't forget that not many years ago a webpage had to reload to show new content :P – 0x_Anakin Jun 26 '15 at 12:14
0

You must use async AJAX calls. Right now, the user interaction is blocked while the HTTP ajax request is being done.

Q: "how professional websites (such as cleartrip.com) provide a smooth client side while processing the server side."

A: By using async AJAX calls

tomtastico
  • 6,136
  • 2
  • 23
  • 28