Jump to content

high memory usage


alzami

Recommended Posts

browser detecting problem .before that codes works fine.its job is to make a div up and down within a period of time.after 10/12 times it crashes?what is the reason and how can it be fixed?

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#its{	width:200px;	height:0px;	background-color:blue;	display:block;	color:white;}#h{	background-color:green;}</style></head><body><div id="its">this is it</div><input type="button" value="down" onClick="view();" id="h"><script>var i=100;function view(){    var m=document.getElementById("its");	  var n=document.getElementById("h");	m.style.webkitTransition="height .5s ease-in-out";    m.style.height="100px";	n.value="up"		setTimeout(function(){		m.style.height="0px";		n.value="down";		},600);		setInterval(view,1200); } </script></body></html>
Edited by alzami
Link to comment
Share on other sites

When you're calling view(), it creates a setInterval that calls itself 1.2 seconds later. then at that time the 1st setInterval would call view again and a NEW setInterval on the view method would be called. at 2.4 seconds view runs twice and creates 2 more setIntervals.

 

at 12 seconds theres over 1,000 setIntervals registered and view is running 1,024 times in that 1.2 second window. if another 12 seconds goes by it would have to run view() more than a million times…..in a second... which if it falls behind stacks up really quickly.

 

 

You need to be EXTRA careful when you're having setTimeout and setInterval recursively call the function they are in. Honestly though, that applies to recursive functions in general.

 

 

remove setInterval from the view function and just put it directly in the onclick.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...