简体   繁体   English

JavaScript setTimeout 无递归无限循环

[英]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.我不能使用Obfuscator.io来丑化我的 JS 脚本,因为它在调用自身的函数中包含一个setTimeout
MCVE: MCVE:

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

Custom obfuscation settings required to reproduce:重现所需的自定义混淆设置:
- Identifier Names Generator: Mangled - 标识符名称生成器: Mangled
- Reserved Names: $ - jQuery - 保留名称: $ - jQuery

Obfuscator.io's error message: Obfuscator.io 的错误信息:

Error: @postConstruct error in class t: @postConstruct error in class t: Maximum call stack size exceeded错误:类 t 中的 @postConstruct 错误:类 t 中的 @postConstruct 错误:超出最大调用堆栈大小

I've read a few other Stack Overflow questions about this.我已经阅读了其他一些关于此的 Stack Overflow 问题。 I understand that calling setTimeout(func) inside func is not actually recursion. 据我所知,调用setTimeout(func)func实际上不是递归。

But still, Obfuscator.io's algorithm can't handle a self-invoking setTimeout delay.但是, Obfuscator.io 的算法仍然无法处理自调用setTimeout延迟。

How do I make a repeatedly-executing function using setTimeout without calling it in the function itself?如何使用setTimeout创建一个重复执行的函数而不在函数本身中调用它? I don't want to use setInterval because I want the delay to begin each time after the function's code has run.我不想使用setInterval因为我希望每次在函数代码运行后开始延迟。 setInterval ignores that. setInterval忽略了这一点。

I think your issue is actually in the use of我认为你的问题实际上是在使用

  • Reserved Names: $ - jQuery保留名称: $ - 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.禁用混淆和标识符的生成,这些标识符由传递的 RegExp 模式匹配。

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.例如,如果您添加^someName ,混淆器将确保以 someName 开头的所有变量、函数名称和函数参数不会被破坏。

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答案:将代码中的 $ 更改为 jQuery,因为混淆器有保留字

Reccomendation: the best way - use uglifyJS instead of obfuscator推荐:最好的方法 - 使用uglifyJS而不是混淆器

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

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