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

  • Home
  • SEARCH
  • 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 8323763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:49:22+00:00 2026-06-08T23:49:22+00:00

Here’s my function. function duplicate_step_through_highlighted (element_jq, target_jq, char_cb) { console.log( element_jq); var contents =

  • 0

Here’s my function.

    function duplicate_step_through_highlighted (element_jq, target_jq, char_cb) {
        console.log( element_jq);
        var contents = element_jq.contents();
        for (var i = 0 ; i < contents.length; ++i) {
            // if text node, step
            if (contents[i].nodeType === 3) {
                // insert empty text node
                var new_tn = document.createTextNode('');
                target_jq.append(new_tn);

                // iterate it 
                var text = contents[i].nodeValue;
                for (var j = 0; j < text.length; j++) {
                    char_cb(text[j],new_tn);
                    new_tn.nodeValue += text[j];
                    // *** I want an async delay here *** 
                }
            } else { // type should be 1: element
                // target_jq gets a duplicate element inserted, copying attrs
                var new_elem = $(contents[i].cloneNode(false)).appendTo(target_jq);
                duplicate_step_through_highlighted($(contents[i]),$(new_elem),char_cb);

                // then a recursive call is performed on the newly created element as target_jq
                // and the existing one as element_jq. char_cb is passed in
            }
        }
    }

What I’m doing is rebuilding an HTML element by reconstructing it one character at a time. There is a good reason for doing this, I want the visual effect of it getting “typed in”.

So right now there are no delays so my element gets duplicated instantly. I have checked that the result is consistent, but it is becoming clear to me that I will probably need to completely re-write the functionality in order for me to be able to put in an asynchronous delay after each character is inserted.

Will I need to re-write it and have a stack to keep track of my position within the elements?

  • 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-06-08T23:49:24+00:00Added an answer on June 8, 2026 at 11:49 pm

    You might want to have a look at my recent answer or this older one (Demo), on how to implement such an effect.


    Tip: Don’t clone the elements into new ones, just hide them and make them appear part-for-part.

    Also, it might be easier not to deal with jQuery instances at all but native DOM elements. So yes, a rewrite might do 🙂 And I think it does need a stack as well.

    function animate(elements, callback) {
    /* get: array with hidden elements to be displayes, callback function */
        var i = 0;
        (function iterate() {
            if (i < elements.length) {
                elements[i].style.display = "block"; // show
                animateNode(elements[i], iterate); 
                i++;
            } else if (callback)
                callback();
        })();
        function animateNode(element, callback) {
            var pieces = [];
            if (element.nodeType==1) {
                while (element.hasChildNodes())
                    pieces.push(element.removeChild(element.firstChild));
                setTimeout(function childStep() {
                    if (pieces.length) {
                        animateNode(pieces[0], childStep); 
                        element.appendChild(pieces.shift());
                    } else
                        callback();
                }, 1000/60);
            } else if (element.nodeType==3) {
                pieces = element.data.match(/.{0,2}/g); // 2: Number of chars per frame
                element.data = "";
                (function addText(){
                    element.data += pieces.shift();
                    setTimeout(pieces.length
                        ? addText
                        : callback,
                      1000/60);
                })();
            }
        }
    }
    
    animate($("#foo").children());
    

    Demo at jsfiddle.net

    How it works:

    • The addText function adds some character to the current text node, and sets a timeout for itself – animation! In case everything is done, it invokes the callback function.
    • childStep runs the animation on a childnode, and passes itself as the callback until no children are left – then nvokes the callback function.
    • Both together, animateNode recursively runs over the node tree and animates the textnodes in thier order.
    • the iterate function calls animateNode (after unhinding them) on all input elements, by passing itself as the callback. After all input elements are finished, it invokes the outer callback which is given as the second argument to animate.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
here is code: <script type=text/javascript> function doit(){ $('table td').each(function () { if ($(this).text().trim() !=
Here is my jquery for dialog </script> <script type=text/javascript> $.ajaxSetup({ cache: false }); $(document).ready(function
Here is the code in a function I'm trying to revise. This example works
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is a simple timepicker to jQuery UI's datepicker <script type=text/javascript> /* <![CDATA[ */
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is a sample of text that I’m working with: Word1 Word2 ... Word4
Here is the code: Function getData(ByVal id As String) Dim reader As SqlClient.SqlDataReader Dim

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.