1

Hiii everyone, I have created a tab based application using html, and I have used setInterval for synchronizing app data from the server, after regular interval of time. But the problem is that, while synchronizing the data, if i switch between tabs, the executing code is stopped and hence sync cannot be completed.

I thought of calling the sync() in different thread, but couldn't get any help on this. Any ideas about how to implement this, so that while switching between tabs, my JS code keeps on executing. Rough structure of my JS code is

setInterval(
sync();
,3000);

function sync()
{
// Code for sync data
}
1
  • 2
    What do you mean different thread? JavaScript is single-threaded (Though there is an HTML5 spec for web workers) Commented Mar 26, 2014 at 16:43

2 Answers 2

4

One of the problems with your example is that you only execute sync a single time (because you added () to it within the setInterval call). Essentially you execute sync once and pass the result in.

Another problem is the ;, which terminates the line.

So removing the brackets and the semi-colon may solve your issue:

setInterval(sync, 3000);

Athough I propose you use a timeout, rather than an interval - in case the request takes longer than three seconds.

function sync() {
    // Code for sync data

    setTimeout(sync, 3000);
}

sync();

On the whole, your JavaScript executes on a single thread using an event loop to execute all of the events in order. Threading won't solve your issue anyway, as it is a slight syntax problem.

Web workers will allow additional threads in browsers and NodeJS also has a threading mechanism - but they should be used sparingly.

Sign up to request clarification or add additional context in comments.

5 Comments

Cordova app is not allowing me to use Workers. Getting error "Worker not found" while creating a constructor for the same. var worker=new Worker("filename.js");
@Bhavna - don't use workers to solve your problem, just correct the syntax error I mentioned.
Thanx for ur help.. but correcting the syntax didn't work.. !! Actually the sync is running if I stay on any one of the pages. As soon as i switch between pages, the sync is stopped. so I want it to be in background thread.. But can't get it !!
@Bhavna - what device are you running on? A lot of "mobile" devices freeze the event loop on a page when it doesn't have focus to keep foreground windows responsive. When you go back to the page it will resume. This isn't something you can code around.
The app is developed for IOS 5+ (universal app) and android minimum SDK 7. The issue persists in all devices.. !!!! And sync is not resumed on moving back to a specific page !!
0

You may want to use Web Workers

The worker thread can perform tasks without interfering with the user interface. In addition, they can perform I/O using XMLHttpRequest (although the responseXML and channel attributes are always null).

You may communicate with a web worker via messages. As an idea, you could spawn a single worker when the page starts, then send it a message with data to upload. It'll silently do it in the background for you.


Another idea is to use Web Sockets. Your browser can listen for incoming messages from a server. The server can broadcast updates to your browser.

2 Comments

You have to be careful with Web Workers though as they are not supported across all browsers. We ran into this issue in a Cordova app we were building when trying to run on iPhone 3GS or below and Android devices below 4.4.
Cordova app is not allowing me to use Workers. Getting error "Worker not found" while creating a constructor for the same. var worker=new Worker("filename.js");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.