setInterval question

P

pcorteen

Hi,

please find below a short example program that is designed to show
successive rows of cells in a browser (firefox) at regular intervals.
My problem is that as the number of cell rows increases the time
interval appears to lengthen making the running program appear to slow
down.

How can I make the cell rows appear at regular intervals without
slowing down?

Thanks in advance for any feedback.

Peter

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<script>
var cellSize=5;
var numCells=100;
var overallWidth=(cellSize+2)*numCells;
var intervalId;
function createCell(){
var cell= document.createElement('div');
cell.style.border='1px solid #ccc';
cell.style.width=cellSize+'px';
cell.style.height=cellSize+'px';
cell.style.cssFloat='left';
cell.style.backgroundColor='#ffc';
return cell;
}
function createCellRow(){
var div=document.createElement('div');
div.style.width=overallWidth+'px';
for (var i=0; i<numCells; i++)
{ div.appendChild(createCell()); }
mainDiv.appendChild(div);
}
function start(){ intervalId=setInterval (createCellRow, 200); }
function stop(){ clearInterval(intervalId); }
function init(){
mainDiv=document.getElementById('mainDiv');
mainDiv.className='mainDiv';
mainDiv.style.width=overallWidth+'px';

var startButton=document.createElement('button');
startButton.innerHTML='Start';
startButton.onclick=start;
mainDiv.appendChild(startButton);

var stopButton=document.createElement('button');
stopButton.innerHTML='Stop';
stopButton.onclick=stop;
mainDiv.appendChild(stopButton);
}
</script>
</head>
<body onload='init()'>
<div id='mainDiv'>
</div>
</body>
</html>
 
D

Darko

Hi,

please find below a short example program that is designed to show
successive rows of cells in a browser (firefox) at regular intervals.
My problem is that as the number of cell rows increases the time
interval appears to lengthen making the running program appear to slow
down.

How can I make the cell rows appear at regular intervals without
slowing down?

Thanks in advance for any feedback.

Peter

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

<script>
var cellSize=5;
var numCells=100;
var overallWidth=(cellSize+2)*numCells;
var intervalId;
function createCell(){
var cell= document.createElement('div');
cell.style.border='1px solid #ccc';
cell.style.width=cellSize+'px';
cell.style.height=cellSize+'px';
cell.style.cssFloat='left';
cell.style.backgroundColor='#ffc';
return cell;}

function createCellRow(){
var div=document.createElement('div');
div.style.width=overallWidth+'px';
for (var i=0; i<numCells; i++)
{ div.appendChild(createCell()); }
mainDiv.appendChild(div);}

function start(){ intervalId=setInterval (createCellRow, 200); }
function stop(){ clearInterval(intervalId); }
function init(){
mainDiv=document.getElementById('mainDiv');
mainDiv.className='mainDiv';
mainDiv.style.width=overallWidth+'px';

var startButton=document.createElement('button');
startButton.innerHTML='Start';
startButton.onclick=start;
mainDiv.appendChild(startButton);

var stopButton=document.createElement('button');
stopButton.innerHTML='Stop';
stopButton.onclick=stop;
mainDiv.appendChild(stopButton);}

</script>
</head>
<body onload='init()'>
<div id='mainDiv'>
</div>
</body>
</html>

Well, you do realize that the computer has its limited memory
resources? And JavaScript is, as much as it is popular, also famous
for being pretty slow. And what you do here is produce 500 div-s per
second, which is not a small job. And as you go further, the more
memory it acquires and the slower it works. You can't really do much
about it, if THIS is what you really need (although I don't see much
use from this, except for exercise)
 
P

pcorteen

Well, you do realize that the computer has its limited memory
resources? And JavaScript is, as much as it is popular, also famous
for being pretty slow. And what you do here is produce 500 div-s per
second, which is not a small job. And as you go further, the more
memory it acquires and the slower it works. You can't really do much
about it, if THIS is what you really need (although I don't see much
use from this, except for exercise)

Hi, thanks for your reply.
The code I supplied just illustrates the essence of my problem. The
program I want to write does more than this. I first program I wrote
to do what I want was written with googles GWT (translates Java code
into javascript) and that program creates the cell rows at regular
intervals for many many rows without any apparent slow down; I wanted
to be able to do the same in Javascript wondering whether I could make
my program go faster but instead I am unable to even get the rows to
appear at regular intervals.
Thanks for looking at my program, do you have any suggestions? - my
GWT effort can be viewed at www.psigenics.co.uk/cellularAutomata/Main.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top