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

时间:2015-06-30 16:26:17

标签: javascript function loops

好的,我的循环模式运行的脚本很少。这是赌博。这是个主意: 我翻转硬币无限次(循环),我有机会50/50双方。但是,在某些时候我会连续5次获得“数字方面”:......,头部,数字,头部,数字,数字,数字,数字,数字...... 并且如果在“数字侧”行中获得5次,我需要限制脚本调用函数。 在我的情况下,我有函数乘法(),并且该函数有时被调用。因此,我需要限制调用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。如果失败,脚本必须繁殖,这是赌博的规则:)

4 个答案:

答案 0 :(得分: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

添加到变量,如果要测试值

 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
}

答案 1 :(得分:1)

好的,我找到了解决方案:

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

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


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

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());
                multiplyCalls = 0; // reset value
        }
});

谢谢@patrick墨菲

所以,我增加计数器&#34;输掉&#34;并重置计数器&#34; win&#34;部分。谢谢大家:))

答案 2 :(得分:0)

你可以像这样模拟一个c风格的块范围的静态变量:

function foobar() {
    this._callCount = this._callCount || 0; // static scoped variable
    if (this._callCount++ >= 5) {
        // reached over 5, exitting.
        return;
    }
    // do something else
}

工作jsfiddle示例: http://jsfiddle.net/z5ojxjke/

答案 3 :(得分:-1)

你不能只为函数调用计数创建一个全局变量,并在每次执行函数时递增它吗?另外,在功能中添加一些条件,当达到极限时会发生什么。