0

I'm trying to create a script to not tire the server with unnecessary requests.

I'm going to detect the windows and tabs opened by the user and then have the server queried by itself and only one card and then pass the server response to the other tabs through the localStorage object.

The problem is that some windows, as shown in this image, do not "update" their timestamp and consequently seem closed when they are not at all. enter image description here

I tried to slow down the MySchedulerRequest.prototype.Loop loop, but the problem continued to persist.

Ideas?

Thanks in advance to everyone!

//*********** MANAGEMENT CACHE

function MyCache(){
  this.Write = function(name, value){
    if(this.Test()) window.localStorage = {};
    value = { val : value }
    window.localStorage[name] = JSON.stringify(value);
  }

  this.Read = function (name){
    if(this.Test()) window.localStorage = {};
    if(!window.localStorage[name]) return '';
    var value = JSON.parse(window.localStorage[name]);
    return value.val;
  }

  this.Test = function(){
    return typeof Storage !== "undefined";
  }
}



//*********** SCHEDULER

var MySchedulerRequest = function(){}

MySchedulerRequest.prototype.isValidId = false;
MySchedulerRequest.prototype.id = false;
MySchedulerRequest.prototype.lastAction = new Date().getTime();
MySchedulerRequest.prototype.timeout = 250;
MySchedulerRequest.prototype.nameStorage = 'Scheduler_MyChat';
MySchedulerRequest.prototype.Fx = function(){};

MySchedulerRequest.prototype.Loop = function(self){

  var newLastAction = new Date().getTime();
  var EXlastAction = new MyCache().Read(self.GetNameStorage());

  if(!EXlastAction == ''){
    if(self.lastAction != EXlastAction) self.genId();
  }

  self.lastAction = newLastAction; 
  new MyCache().Write(self.GetNameStorage(), newLastAction);

  var tabList = self.GetListTab();
  for(var k in tabList){
    if((new Date().getTime() - tabList[k]) > (self.timeout * 4)) window.localStorage.removeItem(k);
  }

  console.log(self.GetListTab());

  setTimeout(function(){ self.Loop(self); }, self.timeout);
}

MySchedulerRequest.prototype.GetListTab = function(){
  var list = {};
  for(var k in window.localStorage){
    if(k.substr(0,this.nameStorage.length) == this.nameStorage) list[k] = new MyCache().Read(k);
  }
  return list;
}

MySchedulerRequest.prototype.GetNameStorage = function(){
  return this.nameStorage + this.id;
}

MySchedulerRequest.prototype.genId = function(){
  this.id = new Date().getTime();
  return this;
}

MySchedulerRequest.prototype.Run = function(){
  this.genId();
  this.Loop(this);
}

EDIT 1: As requested I also add the textual output of the script.

The program correctly detects the opening of the new browser windows by editing them with an ID and assigning and updating the timestamp of its tab.

..and if the tab does not update its timestamp for 4 loops, this window will be declared as closed by the user.

And this is where the problem arises: the timestamp of some windows, as reported in the output, do not update their timestamp and thus end up being falsely closed by the user.

Object { Scheduler_MyChat1516604439029: 1516604451398 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604451650 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604451903 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604452155 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604452408 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604452661 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604452913 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604453164 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604453416 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604453668 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604453929 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604454180 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604454448 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604454701 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604454954 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604455207 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604455465 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604455718, Scheduler_MyChat1516604455533: 1516604455534 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604455970, Scheduler_MyChat1516604455533: 1516604455534 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604456231, Scheduler_MyChat1516604455533: 1516604456021 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604456483, Scheduler_MyChat1516604455533: 1516604456021 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604456735, Scheduler_MyChat1516604456523: 1516604456523, Scheduler_MyChat1516604455533: 1516604456021 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604456987, Scheduler_MyChat1516604456523: 1516604456888, Scheduler_MyChat1516604455533: 1516604456021, Scheduler_MyChat1516604456914: 1516604456913 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604457240, Scheduler_MyChat1516604456523: 1516604456888, Scheduler_MyChat1516604455533: 1516604457142, Scheduler_MyChat1516604456914: 1516604456913 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604457492, Scheduler_MyChat1516604456523: 1516604456888, Scheduler_MyChat1516604455533: 1516604457142, Scheduler_MyChat1516604456914: 1516604456913 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604457745, Scheduler_MyChat1516604456523: 1516604456888, Scheduler_MyChat1516604457536: 1516604457536, Scheduler_MyChat1516604455533: 1516604457142, Scheduler_MyChat1516604456914: 1516604456913 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604457998, Scheduler_MyChat1516604457536: 1516604457536, Scheduler_MyChat1516604455533: 1516604457142 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604458250, Scheduler_MyChat1516604457536: 1516604457536 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604458503, Scheduler_MyChat1516604457536: 1516604457536 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604458756 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604459009 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604459262, Scheduler_MyChat1516604456523: 1516604459026, Scheduler_MyChat1516604455533: 1516604459159, Scheduler_MyChat1516604456914: 1516604459074 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604459514, Scheduler_MyChat1516604456523: 1516604459026, Scheduler_MyChat1516604455533: 1516604459159, Scheduler_MyChat1516604456914: 1516604459074 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604459765, Scheduler_MyChat1516604456523: 1516604459026, Scheduler_MyChat1516604457536: 1516604459554, Scheduler_MyChat1516604455533: 1516604459159, Scheduler_MyChat1516604456914: 1516604459074 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604460018, Scheduler_MyChat1516604456523: 1516604459026, Scheduler_MyChat1516604457536: 1516604459554, Scheduler_MyChat1516604455533: 1516604459159, Scheduler_MyChat1516604456914: 1516604459074 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604460270, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604459554, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604460522, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604459554, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604460774, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604461026, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604461278, Scheduler_MyChat1516604456523: 1516604461036, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604461162, Scheduler_MyChat1516604456914: 1516604461082 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604461531, Scheduler_MyChat1516604456523: 1516604461036, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604461162, Scheduler_MyChat1516604456914: 1516604461082 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604461539, Scheduler_MyChat1516604439029: 1516604461784, Scheduler_MyChat1516604456523: 1516604461036, Scheduler_MyChat1516604457536: 1516604461557, Scheduler_MyChat1516604455533: 1516604461162, Scheduler_MyChat1516604456914: 1516604461082 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604461539, Scheduler_MyChat1516604439029: 1516604462036, Scheduler_MyChat1516604457536: 1516604461557, Scheduler_MyChat1516604455533: 1516604461162, Scheduler_MyChat1516604456914: 1516604461082 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604461539, Scheduler_MyChat1516604439029: 1516604462289, Scheduler_MyChat1516604456523: 1516604462039, Scheduler_MyChat1516604457536: 1516604461557, Scheduler_MyChat1516604455533: 1516604462164, Scheduler_MyChat1516604456914: 1516604462084 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604462542, Scheduler_MyChat1516604456523: 1516604462039, Scheduler_MyChat1516604457536: 1516604461557, Scheduler_MyChat1516604455533: 1516604462164, Scheduler_MyChat1516604456914: 1516604462084 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604462795, Scheduler_MyChat1516604456523: 1516604462039, Scheduler_MyChat1516604457536: 1516604462560, Scheduler_MyChat1516604455533: 1516604462164, Scheduler_MyChat1516604456914: 1516604462084 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604463048, Scheduler_MyChat1516604456523: 1516604463041, Scheduler_MyChat1516604457536: 1516604462560, Scheduler_MyChat1516604455533: 1516604462164, Scheduler_MyChat1516604456914: 1516604462084 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604463300, Scheduler_MyChat1516604456523: 1516604463041, Scheduler_MyChat1516604457536: 1516604462560, Scheduler_MyChat1516604455533: 1516604463167, Scheduler_MyChat1516604456914: 1516604463087 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604463553, Scheduler_MyChat1516604456523: 1516604463041, Scheduler_MyChat1516604457536: 1516604462560, Scheduler_MyChat1516604455533: 1516604463167, Scheduler_MyChat1516604456914: 1516604463087 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604463557, Scheduler_MyChat1516604439029: 1516604463805, Scheduler_MyChat1516604456523: 1516604463041, Scheduler_MyChat1516604457536: 1516604463563, Scheduler_MyChat1516604455533: 1516604463167, Scheduler_MyChat1516604456914: 1516604463087 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604463557, Scheduler_MyChat1516604439029: 1516604464058, Scheduler_MyChat1516604456523: 1516604464043, Scheduler_MyChat1516604457536: 1516604463563, Scheduler_MyChat1516604455533: 1516604463167, Scheduler_MyChat1516604456914: 1516604463087 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604463557, Scheduler_MyChat1516604439029: 1516604464310, Scheduler_MyChat1516604456523: 1516604464043, Scheduler_MyChat1516604457536: 1516604463563, Scheduler_MyChat1516604455533: 1516604464168, Scheduler_MyChat1516604456914: 1516604464090 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604464559, Scheduler_MyChat1516604439029: 1516604464563, Scheduler_MyChat1516604456523: 1516604464043, Scheduler_MyChat1516604455533: 1516604464168, Scheduler_MyChat1516604456914: 1516604464090 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604464559, Scheduler_MyChat1516604439029: 1516604464816, Scheduler_MyChat1516604456523: 1516604464043, Scheduler_MyChat1516604457536: 1516604464565, Scheduler_MyChat1516604455533: 1516604464168, Scheduler_MyChat1516604456914: 1516604464090 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604464559, Scheduler_MyChat1516604439029: 1516604465068, Scheduler_MyChat1516604456523: 1516604465045, Scheduler_MyChat1516604457536: 1516604464565, Scheduler_MyChat1516604455533: 1516604464168, Scheduler_MyChat1516604456914: 1516604464090 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604464559, Scheduler_MyChat1516604439029: 1516604465320, Scheduler_MyChat1516604456523: 1516604465045, Scheduler_MyChat1516604457536: 1516604464565, Scheduler_MyChat1516604455533: 1516604465169, Scheduler_MyChat1516604456914: 1516604465092 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604465562, Scheduler_MyChat1516604439029: 1516604465573, Scheduler_MyChat1516604456523: 1516604465045, Scheduler_MyChat1516604457536: 1516604465566, Scheduler_MyChat1516604455533: 1516604465169, Scheduler_MyChat1516604456914: 1516604465092 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604465562, Scheduler_MyChat1516604439029: 1516604465825, Scheduler_MyChat1516604456523: 1516604465045, Scheduler_MyChat1516604457536: 1516604465566, Scheduler_MyChat1516604455533: 1516604465169, Scheduler_MyChat1516604456914: 1516604465092 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604465562, Scheduler_MyChat1516604439029: 1516604466077, Scheduler_MyChat1516604456523: 1516604466046, Scheduler_MyChat1516604457536: 1516604465566, Scheduler_MyChat1516604455533: 1516604465169, Scheduler_MyChat1516604456914: 1516604465092 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604465562, Scheduler_MyChat1516604439029: 1516604466328, Scheduler_MyChat1516604456523: 1516604466046, Scheduler_MyChat1516604457536: 1516604465566, Scheduler_MyChat1516604455533: 1516604466171, Scheduler_MyChat1516604456914: 1516604466093 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604466565, Scheduler_MyChat1516604439029: 1516604466581, Scheduler_MyChat1516604456523: 1516604466046, Scheduler_MyChat1516604457536: 1516604466570, Scheduler_MyChat1516604455533: 1516604466171, Scheduler_MyChat1516604456914: 1516604466093 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604466565, Scheduler_MyChat1516604439029: 1516604466834, Scheduler_MyChat1516604456523: 1516604466046, Scheduler_MyChat1516604457536: 1516604466570, Scheduler_MyChat1516604455533: 1516604466171, Scheduler_MyChat1516604456914: 1516604466093 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604466565, Scheduler_MyChat1516604439029: 1516604467086, Scheduler_MyChat1516604456523: 1516604467047, Scheduler_MyChat1516604457536: 1516604466570, Scheduler_MyChat1516604455533: 1516604466171, Scheduler_MyChat1516604456914: 1516604466093 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604466565, Scheduler_MyChat1516604439029: 1516604467339, Scheduler_MyChat1516604456523: 1516604467047, Scheduler_MyChat1516604457536: 1516604466570, Scheduler_MyChat1516604455533: 1516604467173, Scheduler_MyChat1516604456914: 1516604467094 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604467591, Scheduler_MyChat1516604456523: 1516604467047, Scheduler_MyChat1516604457536: 1516604467573, Scheduler_MyChat1516604455533: 1516604467173, Scheduler_MyChat1516604456914: 1516604467094 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604467844, Scheduler_MyChat1516604456523: 1516604467047, Scheduler_MyChat1516604457536: 1516604467573, Scheduler_MyChat1516604455533: 1516604467173, Scheduler_MyChat1516604456914: 1516604467094 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604468097, Scheduler_MyChat1516604456523: 1516604468049, Scheduler_MyChat1516604457536: 1516604467573, Scheduler_MyChat1516604455533: 1516604467173 }
export.php:88:3
Object { Scheduler_MyChat1516604439029: 1516604468349, Scheduler_MyChat1516604456523: 1516604468049, Scheduler_MyChat1516604457536: 1516604467573, Scheduler_MyChat1516604455533: 1516604468175, Scheduler_MyChat1516604456914: 1516604468099 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604468573, Scheduler_MyChat1516604439029: 1516604468602, Scheduler_MyChat1516604456523: 1516604468049, Scheduler_MyChat1516604457536: 1516604468575, Scheduler_MyChat1516604455533: 1516604468175, Scheduler_MyChat1516604456914: 1516604468099 }

As a consequence, the desired output must be like the following.

Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604460522, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604459554, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604460774, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604461026, Scheduler_MyChat1516604456523: 1516604460028, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604460160, Scheduler_MyChat1516604456914: 1516604460080 }
export.php:88:3
Object { Scheduler_MyChat1516604460520: 1516604460520, Scheduler_MyChat1516604439029: 1516604461278, Scheduler_MyChat1516604456523: 1516604461036, Scheduler_MyChat1516604457536: 1516604460556, Scheduler_MyChat1516604455533: 1516604461162, Scheduler_MyChat1516604456914: 1516604461082 }

EDIT 2: Here I have brought you an example of the script and to see it in action you need to run it simultaneously on multiple tabs. https://jsfiddle.net/actv4rdk/

EDIT 3: By doing a lot of testing it was found that replacing the setTimeout function with the setInterval function the problem seems to disappear almost completely, but the computer's RAM memory is filled in a few minutes. what has this memory consumption become to say the least disproportionate?

EDIT 4: The bug that filled the entire RAM memory was caused by my inattention and using the setInterval function the program seems stable using my hardware and with waiting times of more than 1000 milliseconds, while some errors still occur under 500 milliseconds .

how is it possible to make the script completely stable using any hardware?

//*********** MANAGEMENT CACHE
function MyCache() {
    this.Write = function(name, value) {
        if (this.Test()) window.localStorage = {};
        value = {
            val: value
        }
        window.localStorage[name] = JSON.stringify(value);
    }

    this.Read = function(name) {
        if (this.Test()) window.localStorage = {};
        if (!window.localStorage[name]) return '';
        var value = JSON.parse(window.localStorage[name]);
        return value.val;
    }

    this.Test = function() {
        return typeof Storage !== "undefined";
    }
};


//*********** SCHEDULER

var MySchedulerRequest = function() {}

MySchedulerRequest.prototype.isValidId = false;
MySchedulerRequest.prototype.id = false;
MySchedulerRequest.prototype.lastAction = new Date().getTime();
MySchedulerRequest.prototype.timeout = 1000;
MySchedulerRequest.prototype.nameStorage = 'Scheduler_MyChat';
MySchedulerRequest.prototype.Fx = function() {};

MySchedulerRequest.prototype.Loop = function(self) {

    var newLastAction = new Date().getTime();
    var EXlastAction = new MyCache().Read(self.GetNameStorage());

    if (!EXlastAction == '') {
        if (self.lastAction != EXlastAction) self.genId();
    }

    self.lastAction = newLastAction;
    new MyCache().Write(self.GetNameStorage(), newLastAction);

    var tabList = self.GetListTab();
    for (var k in tabList) {
        if ((new Date().getTime() - tabList[k]) > (self.timeout * 4)) window.localStorage.removeItem(k);
    }

    console.log(self.GetListTab());

}

MySchedulerRequest.prototype.GetListTab = function() {
    var list = {};
    for (var k in window.localStorage) {
        if (k.substr(0, this.nameStorage.length) == this.nameStorage) list[k] = new MyCache().Read(k);
    }
    return list;
}

MySchedulerRequest.prototype.GetNameStorage = function() {
    return this.nameStorage + this.id;
}

MySchedulerRequest.prototype.genId = function() {
    this.id = new Date().getTime();
    return this;
}

MySchedulerRequest.prototype.Run = function() {
    var self = this;
    this.genId();
    setInterval(function() {
        self.Loop(self);
    }, self.timeout);
}

new MySchedulerRequest().Run(); // start program
Pinguto
  • 416
  • 3
  • 17
  • 2
    In what way does *getTime* not work? What value are you expecting? What do you get? It's used 4 times in the code, which one doesn't work? Do they all fail? You need to post minimal code that displays the issue, see [*How to create a minimal, complete and verifiable example*](https://stackoverflow.com/help/mcve). Don't post images, post text. Not everyone can see images. – RobG Jan 22 '18 at 06:51
  • I modified my question. thanks for your directions .. – Pinguto Jan 22 '18 at 07:39

2 Answers2

1

Two problems here. The first is a typo. You forgot a ! so weird things are gonna happen:

if(!EXlastAction == ''){

This will return true when EXlastAction is not an empty string, which I think is the opposite you want to check. If you want to actually check if is not an empty string, the "official" (and not weird) way of doing it is with:

if(EXlastAction != ''){

And second. Your problem is happening because the browsers will defer any callbacks to run one time per second when the user is not focused on the tab, and having an update of 250 ms that's exactly 4 updates per second. Just the needed to get a page deleted.

This is to prevent the browser slowing down when you have multiple tabs open: Chrome: timeouts/interval suspended in background tabs?

There are some workarounds, but I won't count on them as seems hacks that will change over time, so the best you can do is to keep your interval at 1000 ms.

There's also a new visibility API if you want to do specific logic depending of tab visibility, but notice that not all browsers have it implemented so if you want backwards compatibility for IE 9 and below, you must not totally depend on this functionality, but simply use it to increase your app performance.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
  • You are right! thank you so much! I had not noticed that **if (! EXlastAction == '')** ! Actually he had remained there by mistake, so I proceeded to remove it. I also ignored that the browsers slow down the inactive cards, so at least for the time being I will use times equal to or greater than one second. thank you very much again .. =) – Pinguto Jan 29 '18 at 02:12
0

Not quite sure, but I think you're asking why repeated calls to new Date().getTime() in a tight loop do not always give you a different value.

Indeed this is the case, and is normal. new Date().getTime(), or Date.now() which does the same thing without an allocation, uses the system's realtime clock to give you the time of day. It is not designed for high precision. This is easy to demonstrate in Chrome's debug console:

screenshot

Since Chrome groups the output of identical items, you can easily see how many times it gave the same result when run on my computer.

One way to deal with this is to use performance.now() instead, which doesn't use the realtime clock, but instead uses higher-precision timers (such as QPC).

screenshot

Image truncated, as there are now 100 distinctly different items being returned.

See also performance.now() vs Date.now()

Also, you mentioned you replaced setTimeout with setInterval, be careful - the former will call the given function only once, while the latter will continue to invoke the given function over and over again. They are not interchangeable.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • As explained in the question, most probably the problem is due to the **MySchedulerRequest.prototype.Loop** function which for some reason is not performed with a constant frequency in all the tab. Using the **setIntervall** function the problem is attenuated, but not solved. – Pinguto Jan 27 '18 at 14:28
  • And if you replace all your `new Date().getTime()` statements with `performance.now()`, what happens then? – Matt Johnson-Pint Jan 27 '18 at 18:06
  • It happens that the script will be less backwards compatible with the old browsers and the final result will be the same. – Pinguto Jan 27 '18 at 19:00
  • The results will not be the same. They do different things. It works on IE10+ and all modern browsers. – Matt Johnson-Pint Jan 27 '18 at 19:49
  • The only thing that is known is the different degree of precision and between a loop and the other spend at least 250ms, while in the version with setInterval they spend 1000ms, so what changes? – Pinguto Jan 27 '18 at 20:11