我正在尝试创建一个脚本,以免因不必要的请求而厌倦服务器。
我将检测用户打开的窗口和标签,然后让服务器自己查询,只有一张卡,然后通过localStorage对象将服务器响应传递给其他标签。
问题是,如图中所示,某些窗口不会“更新”它们的时间戳,因此当它们完全没有时就会关闭。
我试图放慢 MySchedulerRequest.prototype.Loop 循环,但问题仍然存在。
想法?
先感谢大家!
//*********** 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);
}
编辑1: 根据要求,我还添加了脚本的文本输出。
程序通过使用ID编辑并分配和更新其选项卡的时间戳来正确检测新浏览器窗口的打开。
..如果标签没有更新4个循环的时间戳,则该窗口将被用户声明为关闭。
这就是出现问题的地方:输出中报告的某些窗口的时间戳不会更新其时间戳,从而最终被用户错误地关闭。
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 }
因此,所需的输出必须如下所示。
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 }
编辑2: 在这里,我给您带来了一个脚本示例,并在运行中看到它需要在多个选项卡上同时运行它。 https://jsfiddle.net/actv4rdk/
编辑3: 通过大量测试,发现用 setInterval 函数替换 setTimeout 函数,问题似乎几乎完全消失,但计算机的几分钟内就会填写 RAM内存。 这种记忆消耗至少可以说是不成比例的吗?
编辑4: 填充整个RAM内存的错误是由于我的注意力不集中而且使用setInterval函数程序似乎稳定使用我的硬件并且等待时间超过1000毫秒,而一些错误仍然在500毫秒内发生
如何使用任何硬件使脚本完全稳定?
//*********** 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
答案 0 :(得分:1)
这里有两个问题。首先是拼写错误。你忘记了一个!
所以奇怪的事情会发生:
if(!EXlastAction == ''){
当EXlastAction
不是空字符串时,这将返回true,我认为这是你要检查的相反。如果你想真正检查一下是不是一个空字符串,"官方" (而不是奇怪的)这样做的方式是:
if(EXlastAction != ''){
第二。您的问题正在发生,因为当用户没有关注选项卡时,浏览器会将任何回调推迟到每秒运行一次,并且更新为250毫秒,即每秒4次更新。只需要删除页面。
这是为了防止在打开多个标签页时浏览器速度变慢:Chrome: timeouts/interval suspended in background tabs?
有一些解决方法,但我不会指望它们看起来会随着时间的推移而变化,所以你能做的最好的事情就是让你的间隔保持在1000毫秒。
如果您想根据标签可见性执行特定逻辑,还有一个新的visibility API,但请注意并非所有浏览器都实现了它,因此如果您想要向后兼容IE 9及以下版本,那么不能完全依赖此功能,只需使用它来提高您的应用程序性能。
答案 1 :(得分:0)
不太确定,但我想你在问为什么在紧密循环中重复调用new Date().getTime()
并不总能给你一个不同的价值。
确实是这种情况,这是正常的。 new Date().getTime()
或Date.now()
在没有分配的情况下执行相同的操作,使用系统的实时时钟为您提供时间。它不是为高精度而设计的。这很容易在Chrome的调试控制台中演示:
由于Chrome将相同项目的输出分组,因此您可以轻松查看在我的计算机上运行时产生相同结果的次数。
解决此问题的一种方法是使用performance.now()
代替,它不使用实时时钟,而是使用更高精度的定时器(例如QPC)。
图像被截断,因为现在返回了100个截然不同的项目。
另见performance.now() vs Date.now()
另外,你提到你用setTimeout
替换了setInterval
,要小心 - 前者只调用给定的函数一次,而后者将一遍又一遍地继续调用给定的函数。它们不可互换。