Hashken
Hashken

Reputation: 4656

Javascript - send user location data to server at regular intervals (mulit-threading)

I have a javascript code that among other things has to periodically send user's geolocation data to the server.

If this had been C or Java, I would have just spawned another thread for this task, while the whole of other functionality takes place in a parallel thread.

But considering that Javascript does not support multi-threading how do I accomplish this task?

I found a few solutions pointing to web workers, but given that for IE only IE10+ supports it, I am looking for a more versatile solution.

Upvotes: 0

Views: 422

Answers (3)

Vitaly Leskiv
Vitaly Leskiv

Reputation: 448

var sender = function(interval, url, collectGeoData) {
  this.interval = interval;
  this.url = url;

  if (typeof collectGeoData == 'function' ) {
        this.collectGeoData = collectGeoData;
  }
  else return null;

  setInterval(send, this.interval);
}

sender.prototype.send = function() {
  var data = this.collectGeoData();
  $.post(this.url, data);
}

var thread = new sender('...',50000, function() { //collecting geo data })`enter code here`;

Upvotes: 0

jvilhena
jvilhena

Reputation: 1131

Javascript is single-threaded. But you can use setInterval to "simulate" multi-threading.

function f() {
    send-geolocation
}
setInterval(f, 1000)

or

setInterval("send-geolocation()", 1000)

Upvotes: 2

Gokul Jai
Gokul Jai

Reputation: 7

You can use the setTimeout and clearTimeout Functions which almost supported by all of the browsers.

by this you can call a recursive Function which does the task of sending Locations.

$.ajax() the jquery function can be used to communicate to the server asynchronously.

Upvotes: 0

Related Questions