gpt4 book ai didi

javascript - 限制在 JS 中调用某些函数的次数

转载 作者:行者123 更新时间:2023-11-30 16:46:38 29 4
gpt4 key购买 nike

好的,我有一个以循环模式运行的小脚本。是来赌的这是想法:我抛硬币无限次(循环),双方都有 50/50 的机会。但是,在某些时候,我会在“数字端”行中获得 5 次:...,head,number,head,NUMBER,NUMBER,NUMBER,NUMBER,NUMBER,...如果在“数字端”行中获得 5 次,我需要限制脚本调用函数。就我而言,我有函数 multiply() 并且有时会调用该函数。因此,我需要限制调用 multiply() 函数的次数,例如。 5 或 4..因此,如果脚本连续调用函数 multiply() 5 次,它会做一些事情(例如显示警报或其他事情)。这是脚本:

var startValue = '0.00000001', // Don't lower the decimal point more than 4x of current balance
stopPercentage = 0.001, // In %. I wouldn't recommend going past 0.08
maxWait = 500, // In milliseconds
stopped = false,
stopBefore = 3; // In minutes

var $loButton = $('#double_your_btc_bet_lo_button'),
$hiButton = $('#double_your_btc_bet_hi_button');

function multiply(){
var current = $('#double_your_btc_stake').val();
var multiply = (current * 2).toFixed(8);
$('#double_your_btc_stake').val(multiply);
}

function getRandomWait(){
var wait = Math.floor(Math.random() * maxWait ) + 100;

console.log('Waiting for ' + wait + 'ms before next bet.');

return wait ;
}

function startGame(){
console.log('Game started!');
reset();
$loButton.trigger('click');
}

function stopGame(){
console.log('Game will stop soon! Let me finish.');
stopped = true;
}

function reset(){
$('#double_your_btc_stake').val(startValue);
}

// quick and dirty hack if you have very little bitcoins like 0.0000001
function deexponentize(number){
return number * 1000000;
}

function iHaveEnoughMoni(){
var balance = deexponentize(parseFloat($('#balance').text()));
var current = deexponentize($('#double_your_btc_stake').val());

return ((balance*2)/100) * (current*2) > stopPercentage/100;
}

function stopBeforeRedirect(){
var minutes = parseInt($('title').text());

if( minutes < stopBefore )
{
console.log('Approaching redirect! Stop the game so we don\'t get redirected while loosing.');
stopGame();

return true;
}

return false;
}

// Unbind old shit
$('#double_your_btc_bet_lose').unbind();
$('#double_your_btc_bet_win').unbind();

// Loser
$('#double_your_btc_bet_lose').bind("DOMSubtreeModified",function(event){
if( $(event.currentTarget).is(':contains("lose")') )
{
console.log('You LOST! Multiplying your bet and betting again.');

multiply();

setTimeout(function(){
$loButton.trigger('click');
}, getRandomWait());

//$loButton.trigger('click');
}
});

// Winner
$('#double_your_btc_bet_win').bind("DOMSubtreeModified",function(event){
if( $(event.currentTarget).is(':contains("win")') )
{
if( stopBeforeRedirect() )
{
return;
}

if( iHaveEnoughMoni() )
{
console.log('You WON! But don\'t be greedy. Restarting!');

reset();

if( stopped )
{
stopped = false;
return false;
}
}
else
{
console.log('You WON! Betting again');
}

setTimeout(function(){
$loButton.trigger('click');
}, getRandomWait());
}
});

如果在“失败”行中获得 5 次,我需要它来停止越来越多的失分,因为如果失败,脚本总是乘以 2。如果输了,脚本必须成倍增加,这是赌博的规则:)

最佳答案

创建全局变量:

var startValue = '0.00000001', // Don't lower the decimal point more than 4x of current balance
stopPercentage = 0.001, // In %. I wouldn't recommend going past 0.08
maxWait = 500, // In milliseconds
stopped = false,
stopBefore = 3, // In minutes
multiplyCalls = 0; // <--- Added this global

添加到变量和 If 到测试值

 function multiply(){
if(multiplyCalls <= 5){ // test multiply
var current = $('#double_your_btc_stake').val();
var multiply = (current * 2).toFixed(8);
$('#double_your_btc_stake').val(multiply);
multiplyCalls++; // increment
}else{
console.log('you lost');
}
}

在游戏重置时重置变量

function reset(){
$('#double_your_btc_stake').val(startValue);
multiplyCalls = 0; // reset value
}

关于javascript - 限制在 JS 中调用某些函数的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31143564/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com