javascript - Nested setTimeouts with the same name , any repercussions doing this ? -
i going through source code of typed.js , main function in plugin uses design pattern of multiple settimeout's nested inside 1 another, have @ code:
self.timeout = settimeout(function() { // check escape character before pause value // format: \^\d+ .. eg: ^1000 .. should able print ^ using ^^ // single ^ removed string var charpause = 0; var substr = curstring.substr(curstrpos); if (substr.charat(0) === '^') { var skip = 1; // skip atleast 1 if (/^\^\d+/.test(substr)) { substr = /\d+/.exec(substr)[0]; skip += substr.length; charpause = parseint(substr); } // strip out escape character , pause value they're not printed curstring = curstring.substring(0, curstrpos); } if (self.contenttype === 'html') { // skip on html tags while typing var curchar = curstring.substr(curstrpos).charat(0); if (curchar === '<' || curchar === '&') { var tag = ''; var endtag = ''; if (curchar === '<') { endtag = '>'; } else { endtag = ';'; } while (curstring.substr(curstrpos).charat(0) !== endtag) { tag += curstring.substr(curstrpos).charat(0); curstrpos++; } curstrpos++; tag += endtag; } } // timeout pause after character self.timeout = settimeout(function() { if (curstrpos === curstring.length) { // fires callback function self.options.onstringtyped(self.arraypos); // final string if (self.arraypos === self.strings.length - 1) { // animation occurs on last typed string self.options.callback(); self.curloop++; // quit if wont loop if (self.loop === false || self.curloop === self.loopcount) return; } self.timeout = settimeout(function() { self.backspace(curstring, curstrpos); }, self.backdelay); } else { /* call before functions if applicable */ if (curstrpos === 0) self.options.prestringtyped(self.arraypos); // start typing each new char existing string // curstring: arg, self.el.html: original text inside element var nextstring = curstring.substr(0, curstrpos + 1); if (self.attr) { self.el.attr(self.attr, nextstring); } else { if (self.isinput) { self.el.val(nextstring); } else if (self.contenttype === 'html') { self.el.html(nextstring); } else { self.el.text(nextstring); } } // add characters 1 one curstrpos++; // loop function self.typewrite(curstring, curstrpos); } // end of character pause }, charpause); // humanized value typing }, humanize);
i don't have problem understanding code above, bit skeptical usage of multiple settimeouts inside each other with same name i.e.
self.timeout
is ok ? there repercussions of using settimeouts nested inside each other ? or kind of coding approach totally fine. had big question mark in mind when saw settimeouts
in code same name.
thank you.
alex-z
there no problem assigning multiple settimeout()
return values same variable self.timeout
.
but lose possibility clear previous timeouts cleartimeout()
self.timeout
got overwritten each time last settimeout()
return value.
however, in specific code example provided cleartimeout()
not used. while missing purpose, assigning return value same variable self.timeout
useless.
Comments
Post a Comment