简体   繁体   English

长期运行的JavaScript网站

[英]Long-running JavaScript Web

I am making a website that contains html5 and JavaScript, The website requires very few user interactions (only at the start), after that the web will display all kind of informations (text, time, pictures, etc) 我正在制作一个包含html5和JavaScript的网站,该网站需要很少的用户交互(仅在开始时),之后该网站将显示所有类型的信息(文本,时间,图片等)

In the website I always use timer (setInterval) at specified intervals (3seconds or even 20seconds), For the first 2 hours, it runs fine, after that, sometimes it will crash the browser or make it hang, Now I wonder if using setInterval is a good idea 在网站上,我总是以指定的间隔(3秒或什至20秒)使用计时器(setInterval),在开始的两个小时内,它运行良好,此后,有时它会使浏览器崩溃或使其挂起,现在我想知道是否使用setInterval是个好主意

I always clear the interval (with clearInterval) if I want to change the interval then activate it again. 如果要更改时间间隔,请始终清除时间间隔(使用clearInterval),然后再次激活它。 I need the web to run days or even weeks, sometimes if it's not hang, it will cause the browser(not the whole PC) to run very slow, 我需要网络运行数天甚至数周,有时即使它没有挂起,也会导致浏览器(不是整个PC)运行非常缓慢,

so is it actually a good idea to use setInterval and clearInterval in the long run? 所以从长远来看,使用setInterval和clearInterval实际上是一个好主意吗? Or should I switch to setTimeout instead (or will they be the same)? 还是应该改用setTimeout(或者它们会相同)?

If the cause is memory leak, could this be the cause? 如果原因是内存泄漏,那可能是原因吗?

var element=document.getElementById("image1"); //it is DIV element
var image=new Image(); 
image.src=url;  // url is a path to a specific image(blob or external source)
image.onload=function(){
//basically
element.style.backgroundImage = "url(" + url + ")";
element.style.width=image.width.toString()+"px"; //set the original size
element.style.height=image.height.toString()+"px";
 //by the end of this function I don't clear the variable image like image=null
};

I found it! 我找到了! Apparently it was not because of the image and DOM creation as I was always handle them correctly after a closure, It was because after some changing of certain DOM Element, I always attach event-handler to an object with jquery ($(element).bind(eventhandler,theHandler);) 显然不是由于图像和DOM的创建,因为我总是在关闭后正确地处理它们,这是因为在某些DOM元素进行某些更改之后,我总是将事件处理程序附加到带有jquery($(element)的对象。绑定(事件处理程序,theHandler);)

I thought when I attach the event with bind, it will replace the handler, but apparently it will stack all og the handlers attached with the new one. 我以为,当我用bind附加事件时,它将替换处理程序,但显然它将所有与新处理程序连接的处理程序堆叠在一起。 So now, I do it only once or unbind the old one if necessary 所以现在,我只执行一次,或者在必要时取消绑定旧的

Afaik there shouldn't be any problems with setInterval , but i guess that you have a memory leak in your source code: Afaik setInterval应该不会有任何问题,但是我想您的源代码中存在内存泄漏:

Check this thread to find tools to help you debug 检查此线程以找到可帮助您调试的工具

Does this go to the GC? 这会转到GC吗?

var a = {}; 
var b = {a: a}; 
a.b = b;

Or this: 或这个:

var a = function(b) { 
    var c = function(b) {}; 
    c(b); 
}
var b = {}; // <-- what about this object?
a(b); 

Maybe you should start rethinking if its a good idea to have a browser running for multiply weeks maybe you should start looking at nodejs ? 也许您应该重新考虑是否将浏览器运行多个星期是否是一个好主意,也许您应该开始研究nodejs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM