0

I'm trying to do some stuff with jQuery and the action is going too slow.

I have an embed (pinterest layout like) collection (pure CSS) in an iOS UIWebView, each pin have a button and this button sends a love over the action. When the user touch the button i have to send the love (unlove) to the server and also refresh the UI (more precisely change a background image).

The problem is that since the user press the button, i'm having almost a second of delay (before the background image change). I will try to avoid this using setTimeout function "emulate" a secondary thread. But didn't work as expect.

What i tried exactly was:

setTimeout(function() {
                love_btn.css("background-image", "url(btn_like_black@2x.png)");
                pin.find(".love").css("background-image", "url(img_like@2x.png)");
                var x = parseInt(pin.find(".counter").first().text());
                var u = x - 1;
                pin.find(".counter").first().text(u);
            }, 0);

            window.location.href = "protocol://unlove/" + love_id;

Last line is a redirection handled in objective-c code to call the WebService that register the love (unlove).

By the way, i tried the timeout in the server call too. So, the question is: Is there a way to change this background images as soon as posible using jQuery? is the setTimeout function worling is really working as i think?

Thanks.

halbano
  • 1,135
  • 14
  • 34
  • 1
    what you are doing will change it as fast as it possibly could. The only way to make it faster would to make it not have to load the image by pre-loading it at some other time so that it will already be cached by the time you need to show it. – Kevin B Nov 08 '13 at 20:02
  • @KevinB the image is a resource in my XCode Project. What do you think about that? Any idea for caching? – halbano Nov 08 '13 at 20:09
  • I think @KevinB hit the nail on the head. Preload the images. You can try checking out this posting on a method for preloading images with jquery: http://stackoverflow.com/questions/476679/preloading-images-with-jquery – CodeChimp Nov 08 '13 at 20:35
  • Thanks guys, i'll take a look in a while! – halbano Nov 09 '13 at 15:01

0 Answers0