HTML page consuming lots of RAM

Multi tool use
HTML page consuming lots of RAM
Memory usage is high, but nothing is shown. The page keeps loading.
<html>
<title>Array</title>
<body onload="myFunction()">
<center>
<p id="demo">This is an array</p>
</center>
<script>
var globe;
var i = 0;
function myFunction() {
var sl = ["a", "b", "c", "d", "d", "e", "f"];
for (i = 0; i < 7; i) {
globe = sl[i];
setTimeout(pk, 50);
}
}
function pk() {
document.write(globe);
i++;
myFunction();
}
</script>
</body>
</html>
Can someone please explain to me, what's really happening in this script?
for (i = 0; i < 7; i)
add
++
to the for loop
: for(var i = 0;i < 7;i++)
– Taki
Jul 1 at 4:39
++
for loop
for(var i = 0;i < 7;i++)
I'm incrementing the value of "i" in function pk()
– Ritesh95
Jul 1 at 4:44
When you start to ask a question, you must finish it with marking one of them as a right answer, or up voting some of them that help you to understand the truth. I up vote your answer because it help me in some thing for example.
– AmerllicA
Jul 2 at 13:20
4 Answers
4
Informally, you can think of all your js code being executed on a single thread. What setTimeout(f,n) does is to execute function f when at least n milliseconds have passed, and that thread is idle. Since myFunction never returns, as it does not increment i internally, the thread never becomes idle, and pk is never called. The memory is being consumed by a huge amount of scheduled calls.
When you fix this, remember that you also have mutual recursion between pk and myFunction, which will also overload your browser with a huge number of calls.
Simple HTML
never use lot of amount of RAM
, if it's happen you must be sure there is a JavaScript
code for implementing interaction that made this issue.
HTML
RAM
JavaScript
For your special code your problem is definitely your JavaScript
code, your for
loop is wrote badly. see:
JavaScript
for
for(i = 0;i < 7;i) // the last i must be i++
{
globe = sl[i];
setTimeout(pk,50);
}
With third i
your loop act infinitely, so in third section you must declare step, the i++
, change your code and you will see it works awesome.
i
i++
Good luck
Basically, to extrapolate a little bit on the answers above, the variable inside the loop never gets incremented, so therefore the loop will run indefinitely.
You are incrementing i
in your pk()
function, but that function never runs, as your app stalls on the infinite loop.
i
pk()
No, there is only the global i
– Jonas H
Jul 1 at 4:45
you are correct - i missed that there was no declaration in the loop. I'll edit my answer. thanks.
– perrysetgo
Jul 1 at 5:55
Adding i++ in loop solves the problem.
You can even use
for (items in sl){
//whatever code you want
}
This for ... in loop gives you benefit that you can transverse array of any size
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
for (i = 0; i < 7; i)
will run forever– CertainPerformance
Jul 1 at 4:39