简体   繁体   中英

JavaScript setTimeout infinite loop without recursion

I cannot use Obfuscator.io to uglify my JS script because it contains a setTimeout within a function that calls itself.
MCVE:

function repeater() {
    // DO SOME STUFF...
    setTimeout(repeater, 100);
}
repeater();

Custom obfuscation settings required to reproduce:
- Identifier Names Generator: Mangled
- Reserved Names: $ - jQuery

Obfuscator.io's error message:

Error: @postConstruct error in class t: @postConstruct error in class t: Maximum call stack size exceeded

I've read a few other Stack Overflow questions about this. I understand that calling setTimeout(func) inside func is not actually recursion.

But still, Obfuscator.io's algorithm can't handle a self-invoking setTimeout delay.

How do I make a repeatedly-executing function using setTimeout without calling it in the function itself? I don't want to use setInterval because I want the delay to begin each time after the function's code has run. setInterval ignores that.

I think your issue is actually in the use of

  • Reserved Names: $ - jQuery

as using that as the configuration results in this

在此处输入图片说明

Which is what you're getting, if you change it to ^$ which is what the text box and description on the website says it should be, your code obfuscates fine

在此处输入图片说明

Reserved Names

Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns.

For instance, if you add ^someName , the obfuscator will ensure that all variables, function names and function arguments that starts with someName will not get mangled.

I this you have something like that:

function repeater() {
    // DO SOME STUFF...
    const someCodeInJQuery = $('#someId')
    setTimeout(repeater, 100);
}
repeater();

Just need change to:

function repeater() {
    // DO SOME STUFF...
    const someCodeInJQuery = jQuery('#someId'); // Pay attention here
    setTimeout(repeater, 100);
}
repeater();

Anwer: change $ to jQuery in your code, because obfuscator have reserved words

Reccomendation: the best way - use uglifyJS instead of obfuscator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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