问题
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