简体   繁体   English

JavaScript中setTimeout的内存泄漏问题

[英]Memory leak issues with setTimeout in javascript

I have a javascript code to control the animation a moving div.In this I used setTimeout to make the div move. 我有一个JavaScript代码来控制动画的移动div。在此我使用setTimeout来使div移动。 you can click on the following link to play the animation. 您可以单击以下链接播放动画。

     `http://jsfiddle.net/dudling007/PfPzj/3/`

The problem is that after playing the animation, whenever I clicked a couple of time on pause and resume buttons , it takes a lot of memory and the clicking of the pause buttons also become so slow. 问题在于,播放动画后,每当我在暂停和恢复按钮上单击两次时,会占用大量内存,并且暂停按钮的单击也变得很慢。 I saw the %CPU and real memory consumption from the activity monitor of the MAC OSX.Can someone please help me what is the problem in the code that is consuming some much memory after clicking the pause button 10 -14 times. 我从MAC OSX的活动监视器中看到了%CPU和实际内存消耗。有人可以帮助我,单击暂停按钮10 -14次后,占用大量内存的代码是什么问题。

Memory leak happens because $('#pause_button').click event is set every time moveCard method is called. 内存泄漏的发生是因为每次调用moveCard方法时moveCard设置$('#pause_button').click事件。 Button bindings should be moved out of the "object" cycle. 按钮绑定应移出“对象”循环。

You can use tool link firebug for firefox to check console log of javascript. 您可以使用Firefox的工具链接firebug来检查javascript的控制台日志。 Here is your example with log on pause button. 这是您的带有登录暂停按钮的示例。 This should be happen only once. 这应该只发生一次。 http://jsfiddle.net/Th6ha/1/ http://jsfiddle.net/Th6ha/1/

$('#pause_button').click(function() { 
console.log('pause pressed');
                      arrayObject.pause(wStart,lStart,wEnd,lEnd);
                      $('#resume_button').css('display','inline-block');
                      $('#pause_button').css('display','none');

                      }); 

You can consider using method animate of jquery 您可以考虑使用jani的方法动画

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

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