我不是在javascript上玩,但我试图编辑这个用户脚本,以便每按一次键就产生不同的值。
原始代码将var scanFreq和isDoneLoadingFreq设置为精确值,如400.我希望它是随机的,这就是Math函数的原因。但它只触发一次,因此代码将始终重复切换(whichMove)情况1:,case2:等。
我尝试在每种情况下生成随机数,但这些值不会出现在外面。
编辑:isDoneLoadingFreq正在加载setInterval(function(),对吗? 我的目标是每次按下一个键时使该值随机:|
Edit2 :(根据wwv编辑的代码) 一旦发现神奇宝贝,就会触发第一个功能。但它是旧代码,目标是在关闭警报后,代码不应该运行更多。我需要改变什么? (如果您认为缺少某些内容,请检查帖子末尾的pastebin以获取更多代码)
编辑3:编辑该功能以便在警报弹出后停止循环,但它仍在发生
function found(thing){
if($('#pkmnappear').first().html().toLowerCase().indexOf(thing.toLowerCase()) > -1){
alert("Found Something");
finderOn = true;
clearInterval(a);
if(autoWalkFind){
clearInterval(b);
}
return true;
}else{
return false;
}
}
function setFinder(){
finderOn = true;
if(autoWalkFind) {
var b;
var checkLoading = function(){
var isLoading = $("#pkmnappear").text().indexOf("Please wait") > -1;
if(!isLoading){
switch(whichMove){
case 1:
fireKey(document,37);
whichMove = 2;
break;
case 2:
fireKey(document,38);
whichMove = 3;
break;
case 3:
fireKey(document,39);
whichMove = 4;
break;
case 4:
fireKey(document,40);
whichMove = 1;
break;
}
moveTimes=0;
}else{
moveTimes++;
// Not done loading. check again after a random delay
b = setTimeout(checkLoading, Math.floor(Math.random() * 300) + 400);
}
if(moveTimes >= 100)
location.reload(true);
// call setTimeout again with a new delay
b = setTimeout(checkLoading, Math.floor(Math.random() * 300) + 400);
}
// call setTimeout with the first delay
b = setTimeout(checkLoading, Math.floor(Math.random() * 300) + 400);
}
var a;
var scan = function(){
var stopChecking = false;
if(findPokemon)
for (var i = 0; i < pokemonToFind.length; i++)
stopChecking = stopChecking || found(pokemonToFind[i]);
if(findLevels)
for (var i = 0; i < levelsToFind.length; i++)
stopChecking = stopChecking || found("Level: " + levelsToFind[i] + " ");
if(findLevelsAndUp)
for(var l = levelsAndUpToFind; l<101;l++)
if (found("Level: " + l + " ")) {
stopChecking = true;
break;
}
// Nothing found. check again after a random delay
if(!stopChecking) {
a = setTimeout(scan, Math.floor(Math.random() * 100) + 120);
}
}
// call setTimeout with the first delay
a = setTimeout(scan, Math.floor(Math.random() * 100) + 120);
}
setFinder();
这是我正在使用的整个代码的链接,以防有必要:https://pastebin.com/Q0GudL5c
答案 0 :(得分:0)
scanFreq
时才会定义 isDoneLoadingFreq
和setFinder
。为了使其动态化,您必须将其定义为这样的函数,例如
var scanFreq = (function () { return Math.floor(Math.random() * 2000) + 110; });
var math2 = (function () { return Math.floor(Math.random() * 2000) + 400; });
var isDoneLoadingFreq = math2;
然后在setInterval
中拨打setInterval(your_func, scanFreq())
答案 1 :(得分:0)
setInterval
将以设定的频率重复执行代码。我不知道有任何改变频率的方法,所以改变这些变量将没有任何效果。
但是,setTimeout
只执行一次代码。然后,如果您在函数末尾添加setTimeout
个调用,则每次都可以随机生成一个新的延迟。
要做到这一点,你必须将函数存储在变量中。所以你的代码最终会看起来像这样:
function setFinder(){
finderOn = true;
if(autoWalkFind) {
var b;
var checkLoading = function(){
var isLoading = $("#pkmnappear").text().indexOf("Please wait") > -1;
if(!isLoading){
// ...
}else{
moveTimes++;
// Not done loading. check again after a random delay
b = setTimeout(checkLoading, Math.floor(Math.random() * 2000) + 400);
}
// ...
// ...
}
}
var a;
var scan = function(){
var stopChecking = false;
if(findPokemon)
for (var i = 0; i < pokemonToFind.length; i++)
stopChecking = stopChecking || found(pokemonToFind[i]);
if(findLevels)
for (var i = 0; i < levelsToFind.length; i++)
stopChecking = stopChecking || found("Level: " + levelsToFind[i] + " ");
if(findLevelsAndUp)
for(var l = levelsAndUpToFind; l<101;l++)
if (found("Level: " + l + " ")) {
stopChecking = true;
break;
}
// Nothing found. check again after a random delay
if(!stopChecking) {
a = setTimeout(scan, Math.floor(Math.random() * 2000) + 400);
}
}
// call setTimeout with the first delay
a = setTimeout(scan, Math.floor(Math.random() * 2000) + 400);
}