Search code examples
javascriptjqueryajaxcordovaweb-worker

should i use Web Workers or setInterval()


i am using phonegap to build apps. currently i need to control the concurrent user access using the same login id.

i need to send an ajax request to web api server to check whether there is user using the same login id. this ajax request need to be send every 30 second.

if api server return 'Y', means more than 1 person using the same login account, i need to close the apps and return back to login screen.

what i worry is, if i using setInterval(), the ajax call will have impact to the main UI. In this situation, should i use web workers ?


Solution

  • Use Ajax requests. They are asynchronous by default, and waiting for the server to return the response will not in any way impact the main UI.

    Also note there are many techniques that may provide a better user experience than polling every 30 seconds. Check out web sockets if you are interested in an alternative implementation.