Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:16:21+00:00 2026-05-31T12:16:21+00:00

I would like to perform a long running, node-by-node walk of the DOM, calling

  • 0

I would like to perform a long running, node-by-node walk of the DOM, calling a function on each node, but without making the browser unresponsive.
So I’m thinking asynchronous is the way to go.

I think jQuery Deferred objects could provide a solution, but I haven’t managed to come up with one yet.

Can anyone give an example of how you could do this with jQuery (or another library, if one particularly stands out as suitable; or pure Javascript and DOM methods).

To further complicate things, I would like the ability to traverse in different orders, for example postorder, but not yet a requirement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:16:23+00:00Added an answer on May 31, 2026 at 12:16 pm

    You could do this recursively with setTimeout. Since Javascript is single threaded you can create the illusion of having multiple threads by relinquishing the thread periodically. Calling setTimeout will queue the remaining work and process any outstanding events before continuing.

    Here’s a working example with jQuery:

    function traverse(node, visitor, root) {
        if (root === undefined)
            root = node;
    
        visitor(node, function() {
            if (!node.length)
                return;
    
            let nextNode;
            if (node.children().length) {
                nextNode = node.children().first();
            } else {
                nextNode = node;
                do {
                    if (root.length && nextNode.length && nextNode[0] == root[0]) {
                        nextNode = $();
                        break;
                    } else if (nextNode.next().length) {
                        nextNode = nextNode.next();
                        break;
                    } else {
                        nextNode = nextNode.parent();
                    }
                } while (nextNode.length);
            }
    
            setTimeout(function() { traverse(nextNode, visitor, root); }, 200);
        });
    }
    

    This will call visitor with all DOM nodes in depth-first pre-order traversal, then call visitor with an empty jQuery object to indicate that it’s done. visitor must call its second parameter to proceed with traversal:

    traverse($('#rootNode'), function(node, proceed) {
        if (!node.length) {
            console.log('done with traversal');
            // no proceed() call at the end
        } else if (node.hasClass('my-class')) {
            // let's say that this click triggers an AJAX request that makes a node visible later in traversal
            node.trigger('click');
            proceed(); // continue traversal
        } else if (node.hasClass('ajax-response-container')) {
            function checkIfVisible() {
                if (node.is(':visible')) {
                    // yay, ajax response arrived
                    console.log('AJAX response', node.html());
                    // continue traversal
                    proceed();
                } else {
                    // wait for ajax node to become visible
                    window.requestAnimationFrame(checkIfVisible);
                    // no proceed() call - don't continue traversal yet
                }
            }
    
            checkIfVisible();
        } else {
            // continue traversal for any other node encountered
            proceed();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Java, the function I would like to perform is to
I would like to perform a key event detection in textbox. The keys should
I would like to perform something similar to this (ie get the sum of
I would like to perform a bitwise exclusive or of two strings in python,
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to be able to perform some logic in the table.modifiedField method
I would like to post-process my app.config file and perform some token replacements after
I need to perform Diffs between Java strings. I would like to be able
Now while I know that you can not perform inheritance like you would in
How would I go about setting a link to perform certain functions like hide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.