anonychair38
New Member
- Messages
- 8
- Reaction score
- 0
- Points
- 0
JavaScript is a memory hog. Most programmers know this. What a lot of new programmers do not know, is that you cannot create an infinite loop in JavaScript using While/For loops or other conditional statements. Theoretically, it is possible. But, the browser has a way of detecting this. Say for example, my code was:
Since I declared the variable ten as 1, the while condition will always be true. Therefore, it will loop forever. BUT, after a few minutes of looping, the browser will halt the loop, and open a dialogue box. This dialogue box will say something along the lines of "Excessive memory usage. JavaScript on page still running. Continue?" and then prompt the user to either click yes or no. Should the user click no, the JavaScript will stop looping.
Fortunately, there is a way to work around this dialogue using setTimeout, and a recursive function.
What is a recursive function?
Normally, when a function is defined by the programmer, it is so the function can be used elsewhere in the code. A recursive function differs because the function name is called within the function itself. When we combine this with setTimeout, it creates an infinite loop that repeats itself according to the time you set in setTimeout.
So, to execute code every 5 seconds in an infinite loop, our code will be:
As you can see, I have created a function called loop(). I have then called this function within setTimeout, which is already inside the function.
Knowing this is useful if you want to create a slideshow that scrolls through each image and then repeats the process.
Just a tip for beginner programmers.
Code:
ten = 1
while (ten = 1) {
//some code here
}
Since I declared the variable ten as 1, the while condition will always be true. Therefore, it will loop forever. BUT, after a few minutes of looping, the browser will halt the loop, and open a dialogue box. This dialogue box will say something along the lines of "Excessive memory usage. JavaScript on page still running. Continue?" and then prompt the user to either click yes or no. Should the user click no, the JavaScript will stop looping.
Fortunately, there is a way to work around this dialogue using setTimeout, and a recursive function.
What is a recursive function?
Normally, when a function is defined by the programmer, it is so the function can be used elsewhere in the code. A recursive function differs because the function name is called within the function itself. When we combine this with setTimeout, it creates an infinite loop that repeats itself according to the time you set in setTimeout.
So, to execute code every 5 seconds in an infinite loop, our code will be:
Code:
[COLOR=#003366][B]function[/B][/COLOR] loop[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR] [COLOR=#006600][I]// your code[/I][/COLOR] setTimeout[COLOR=#009900]([/COLOR][COLOR=#3366CC]"loop()"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#CC0000]5000[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#006600][I]// 5000 milliseconds = 5 seconds[/I][/COLOR] [COLOR=#009900]}[/COLOR] loop[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
As you can see, I have created a function called loop(). I have then called this function within setTimeout, which is already inside the function.
Knowing this is useful if you want to create a slideshow that scrolls through each image and then repeats the process.
Just a tip for beginner programmers.