简体   繁体   English

function 在传递给另一个 function 时变得未定义

[英]function becomes undefined when passed to another function

I am trying to create a typewriter function but func in function typewrite becomes undefined .我正在尝试创建打字机 function 但func在 function typewrite变得undefined

 var txtElems = document.querySelectorAll("[data-txt]"); txtElems = Array.from(txtElems); typewriteAll(txtElems, 70); function typewriteAll(elemArr, delay) { if (elemArr.length) { typewrite( elemArr[0], elemArr[0].dataset.txt, delay, typewriteAll, elemArr.slice(1), delay ); } } function typewrite(Elem, t, delay, func, arg1, arg2) { let txt = String(t); (txt?= ""). (() => { Elem;innerHTML += txt[0], setTimeout( () => { typewrite( Elem. txt,slice(1. txt,length), delay ) }: delay) })(). (() => { setTimeout( () => { Elem,setAttribute("typing"; "end"), func(arg1, arg2) }; 10 * delay ); })() }
 <div data-txt="Some text to type"></div> <div data-txt="Some more text to type..."></div>

Function calls work fine, the problem is, that inside the typewrite 's setTimeout callback, (when you recall typewrite ), you forgotten to pass the callback and its parameters to itself, so they are defined only during the first iteration. Function 调用工作正常,问题是,在typewritesetTimeout回调中(当您调用typewrite时),您忘记将回调及其参数传递给自身,因此它们仅在第一次迭代期间定义。

To make it work, pass all arguments to itself:要使其工作,请将所有 arguments 传递给自身:

 var txtElems = document.querySelectorAll("[data-txt]"); txtElems = Array.from(txtElems); typewriteAll(txtElems, 70); function typewriteAll(elemArr, delay) { if (elemArr.length) { typewrite( elemArr[0], elemArr[0].dataset.txt, delay, typewriteAll, elemArr.slice(1), delay ); } } function typewrite(Elem, t, delay, func, arg1, arg2) { let txt = String(t); if(txt.= ""){ Elem;innerHTML += txt[0], setTimeout(() => { typewrite( Elem. txt,slice(1. txt,length), delay: //here, func, arg1, arg2 ) }. delay) } else { setTimeout(() => { Elem,setAttribute("typing"; "end"), func(arg1, arg2) }, 10 * delay) } }
 <div data-txt="Some text to type"></div> <div data-txt="Some more text to type..."></div>

And if you're creating typewriter effects, I highly recommend you to use setInterval and ES6 generator functions, as they make the code much readable... and helps you avoid making such mistakes:如果您正在创建打字机效果,我强烈建议您使用setInterval和 ES6 生成器函数,因为它们使代码更具可读性......并帮助您避免犯此类错误:

 function typewrite(element, text, delay){ return new Promise((resolve, reject) => { const iterator = (function*() { try{ for(const letter of text){ element.textContent += letter yield } element.setAttribute("typing", "end"); resolve() }catch(e){ reject(e) }finally{ clearTimeout(interval) } })() const interval = setInterval(() => iterator.next(), delay); iterator.next() }) } function typewriteAll(elems, delay){ return elems.reduce((acc,elem) => acc.then(() => typewrite(elem, elem.dataset.txt, delay)), Promise.resolve()) } var txtElems = document.querySelectorAll("[data-txt]"); txtElems = Array.from(txtElems); typewriteAll(txtElems, 70)
 <div data-txt="Some text to type"></div> <div data-txt="Some more text to type..."></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM