si4 5
New Member
Hello,
I'm currently studying javascript by myself.
I thought that I will write js that will remove element in the body from DOM every 2 seconds, untill all elements in the body are deleted( except debug <span> :) )
At first I tried to go through the elements with loop, but when I call setTimeout() the loop doesn't wait setTimeout() to end, but it continues looping.
Now I use recursion. setTimeout() call the function which do the stuff.
But when there is more nested tag( as in the sample html below <ul><li></li</ul> ) inner recursive function( cDestoyer() at the middle of the function body ) stops for some reason.
And whole ul is deleted, not element by element.
When I don't use setTimeout(), recursion is working correctly.
Any help will be appreciated.
Here is the example: http://www.svubit.eu/iskren/ 4 5226 jx2 228
New Member
i am surprice it works at all!!
your.js file is one line and there are coments in it !! so its meen part of the code is commented... isnt it?
could you post your code?
regards jx2
si4 5
New Member
May be there is problem with line breaks, because file is created with gnu/linux editor and some windows editors show it as one line.
This is the code in the js file: - function cDestroyer( node, it ){
-
if( !node){ var node = document.body }
-
-
var stuffToDestroy = node.childNodes;
-
-
if( it ){ it = parseInt(it);}
-
if( !it ) { it= stuffToDestroy.length - 1;}// we start from the end of the dom tree
-
var target = stuffToDestroy[it];
-
-
if( target ){// no " ... has no properties" error
-
if( target.nodeType == 1)//we need to delete only elements
-
{
-
debug("--node=" +node.nodeName + "; target=" +target.nodeName+ "; it=" +it);
-
if( target.hasChildNodes() ){// if the element has childs we need to delete them first
-
debug("+++++++node(" +target.nodeName+ ") has childs");
-
cDestroyer( target );
-
}
-
if( target.id != "debug" ){ node.removeChild(target);}
-
debug("==End of " +target.nodeName+ " circle");
-
}
-
}
-
it--;
-
if( it > 0 ){
-
//debug(">>End of a big loop");
-
if( useSetTimeout == 1 ){
-
setTimeout(function() { cDestroyer(node,it) }, destroyTimeout * 1000);
-
}else{
-
cDestroyer( node, it);
-
}
-
}
-
-
}
-
-
function debug( msg ){
-
if( showDebug == 1 ){
-
container = document.getElementById('debug');
-
container.innerHTML = msg + "<br />\n"+ container.innerHTML;
-
}
-
}
-
function setSettings( which, val ){
-
if( which == "debug") {
-
showDebug = val;
-
}else {
-
useSetTimeout = val;
-
}
-
}
-
var destroyTimeout = 1;// in secs.
-
var showDebug = 1;
-
var useSetTimeout = 1;
jx2 228
New Member
first of all
setTimeout() doesnt stop the script(you know that allready)
it "reminds" your script that it have to do some code after some time
in your case you should concsider to use setInterval() insted that would make the job
e.g.
[html]<a href="javascrip t:self.setInter val("cDestroyer ()", 2000)">destroy</a>[/html]
(and do not use settimeout() of course)
what did happen in your code: - function cDestroyer( node, it ){
-
if( !node){ var node = document.body }
-
-
var stuffToDestroy = node.childNodes;
-
-
if( it ){ it = parseInt(it);}
-
if( !it ) { it= stuffToDestroy.length - 1;}// we start from the end of the dom tree
-
var target = stuffToDestroy[it];
-
-
if( target ){// no " ... has no properties" error
-
if( target.nodeType == 1)//we need to delete only elements
-
{
-
debug("--node=" +node.nodeName + "; target=" +target.nodeName+ "; it=" +it);
-
if( target.hasChildNodes() ){// if the element has childs we need to delete them first
-
debug("+++++++node(" +target.nodeName+ ") has childs");
-
//===================== here is where the trubles starts ============
-
//cDestroy will destroy "first child " yeah but after 1000 miliseconds
-
cDestroyer( target );
-
//========================================================
-
}
-
if( target.id != "debug" ){ node.removeChild(target);}
-
debug("==End of " +target.nodeName+ " circle");
-
}
-
}
-
it--;
-
if( it > 0 ){
-
//debug(">>End of a big loop");
-
if( useSetTimeout == 1 ){
-
-
//========= thats where the first child will be destroyed next time ======
-
//========= see coments above ==================
-
// ======== after 1000miliseconds but setTimeout() doesnt wait!! ====
-
-
setTimeout(function() { cDestroyer(node,it) },
-
-
-
-
destroyTimeout * 1000);
-
}else{
-
//=================== but you do destroy parent here immeadietly after
-
//=================== you used cdestroy ( child ); ==============
-
-
cDestroyer( node, it);
-
-
// ===now it keep doing the job but cDestroy( yourChild ) is waiting =====
-
//=== and after 1 second there is nothing to destroy because child
-
//=== was destroyed toghether with parent
-
-
}
-
}
-
-
}
-
-
function debug( msg ){
-
if( showDebug == 1 ){
-
container = document.getElementById('debug');
-
container.innerHTML = msg + "<br />\n"+ container.innerHTML;
-
}
-
}
-
function setSettings( which, val ){
-
if( which == "debug") {
-
showDebug = val;
-
}else {
-
useSetTimeout = val;
-
}
-
}
-
var destroyTimeout = 1;// in secs.
-
var showDebug = 1;
-
var useSetTimeout = 1;
i hope i made it clear
post again if you get into truble
regards
jx2
si4 5
New Member
OK. Thanks a lot for the help.
I've though to use setInterval(), but I wanted to understand why this brakes.
Thank you again for the explanation!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jupiter49 |
last post by:
I'm trying to move pictures around on the screen, with a slight delay
between the first picture and second picture (following onmousemove).
When I add the setTimeout function I must be doing it wrong because
the function that calculates the new coordinates for the picture
returns 32 million and change instead of the coordinate. This causes
the picture to not be able to appear on the display, obviously.
Here's the calling code (snipped...
|
by: Gordan |
last post by:
hi
i want to have a "close" button that user can click but that will also
"click itself after 10 sec"
so heres what i did
function auto_close(x)
{
if(x>0){ document.form.button.value="CLOSE (auto close in "+ x +" sec)";
x--;
|
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {
document.images.src = eval("mt" +menu+ ".src")
}
alert("imgOff_hidemenu");
hideMenu=setTimeout('Hide(menu,num)',500);
|
by: Frances Del Rio |
last post by:
this code is supposed to flip imgs at intervals of one second, but it
flipps them much faster, it seems it flips them all in that first
second, the more seconds the faster it flips them, instead of the other
way around... (I mean it seems to me the more seconds I put in there the
more seconds should go by betw. flips... each photo is in a div, each
div has a z-index of 0, 1, 2, etc..)
http://www.francesdelrio.com/cassini
(pls reload a...
|
by: Athanasius |
last post by:
Could someone shed some light as to why the following setTimeout
function will not work on the Mac IE5.2? It does however work on
PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script, it
should simply present an alert message after 5 seconds. Thanks.
<html>
<head>
<script language="javascript">
function MsgTimer() {
var self = this;
| |
by: Andrew Poulos |
last post by:
With the following code I can't understand why this.num keeps
incrementing each time I create a new instance of Foo. For each instance
I'm expecting this.num to alert as 1 but keeps incrementing.
Foo = function(type) {
this.num = 0;
this.type = type
this.trigger();
}
Foo.prototype.trigger = function() {
|
by: Andre |
last post by:
Hi,
Does anyone know whether the ECMA, or an other standard document,
specifies a maximum for the value that can be pass to the setTimeOut()
function in Javascript?
Andre
|
by: pengypenguin |
last post by:
As we know, Javascript suffers an insufferable lack of a wait() or
sleep() function, and so setTimeOut must step in to take up the slack.
A function can use setTimeOut like sleep() by calling itself after x
seconds. This begs a few questions: Is this design building enormous
calling stacks to nowhere? Will the users machine eventually run out of
memory space to contain this stack? How can this be avoided?
Thanks,
-- Whit Nelson
|
by: Muzammil |
last post by:
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}
can any help me ??
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |