0

I am trying to build some re-usable code snippets that create a typewriter effect. It hides a paragraph and then replaces it with another paragraph with some js code that prints one character at a time. I am trying to make the id of the paragraph and the text parameters that I can re-use but I am having trouble allowing these parameters to pass through my function. Some very helpful individuals have helped me thus far but I can't figure out this final step.

I will attach both my function with and then without parameters passing through. Any help would be greatly appreciated.

    <body>

    <div class="style typeClick">
      <p id="remove">Hide Me</p>
      <p id="type"></p>
  </div>
</body>

<style>
  .style {
    height: 2em;
    width: 100%;
    background-color: white;

  }
  body{
    background-color: lightgrey;
  }

  .hide {
    display: none;
  }
</style>

<script>
    /* ---------- DOESNT WORK AS EXPECTED  ---------- */
  (() => {

    function hideInitText() {
      let hide = document.getElementById("remove");
      hide.classList.add("hide");
    }
    hideInitText();
  //make a parameter that will take each id through it

    const typeWriter = ((id, text) => {
      let letCounter = 0;
      return () => {
        let cycle, classCounter;
        let typewriter = text;
        let speed = 50;

        //Cycle through each id after done
        cycle = document.querySelectorAll(id);

        for (classCounter = 0; classCounter < cycle.length; classCounter++) {
          typeOut();
        }


        function typeOut() {
          if (letCounter < typewriter.length) {
            cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
            letCounter++;
            setTimeout(typeWriter, speed);
          }
        }
      };
    })();
    document.querySelector('.typeClick').addEventListener('click', function() {typeWriter("#type", "type out text")});
  })();






      /* ---------- WORKS AS EXPECTED ---------- */


      (() => {

        function hideInitText() {
          let hide = document.getElementById("remove");
          hide.classList.add("hide");
        }
        hideInitText();
      //make a parameter that will take each id through it

        const typeWriter = (() => {
          let letCounter = 0;
          return () => {
            let cycle, classCounter;
            let typewriter = "Type out text";
            let speed = 50;

            //Cycle through each id after done
            cycle = document.querySelectorAll("#type");

            for (classCounter = 0; classCounter < cycle.length; classCounter++) {
              typeOut();
            }

            function typeOut() {
              if (letCounter < typewriter.length) {
                cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
                letCounter++;
                setTimeout(typeWriter, speed);
              }
            }
          };
        })();
        document.querySelector('.typeClick').addEventListener('click', typeWriter());
      })();


</script>

1 Answer 1

1

When using ((id, text) => {})() the function is called with zero argument. If you want to give args to this function please don't use IIFE, or using (id, text) => { ((id, text) => {})(id, text) }.

https://codepen.io/1010543618/pen/MWWLxmN?editors=0110

  const typeWriter = (selector, text) => {
    let letCounter = 0;
    let cycle, classCounter;
    let typewriter = text;
    let speed = 50;

    //Cycle through each id after done
    cycle = document.querySelectorAll(selector);

    function typeOut() {
      if (letCounter < typewriter.length) {
        for (classCounter = 0; classCounter < cycle.length; classCounter++) {
          cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
          letCounter++;
        }
        setTimeout(typeOut, speed);
      }
    }

    typeOut();
  };
Sign up to request clarification or add additional context in comments.

3 Comments

You are a god send. What is the reason not to use an IIFE? Is it still appropriate without params?
According to my understanding, using IIFE means you create a function and call it Immediately. In your case you do var tempfunc = (id, text) => { xxxxx }; var typeWriter = tempfunc(). When your call typeWriter you params give the function returned by tempfunc.
When your call typeWriter the function returned by tempfunc is you really called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.