Passing parameters into this function not working?

无人久伴 提交于 2020-01-06 05:39:10

问题


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:


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();
  };


来源:https://stackoverflow.com/questions/58947819/passing-parameters-into-this-function-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!