使用Javascript中的函数重新定义函数

时间:2016-04-23 15:44:16

标签: javascript

这是一个执行某些操作的脚本,但最重要的是,它会不断地将 inDynamicEditMode 的值从0更改为1,从1更改为0.我试图创建一个如果 inDynamicEditMode 为0(或1),则重新定义函数 payx()的函数。我发布了整个脚本,以防我删除重要内容。

var dataContext = new PetaPoco.Database("sqlserverce");
var Something = dataContext.Query<Models.Something.ClassName>("Query");

以下是两个功能

var start = 0.01

var $input = $("#oddsInput")
var $odds = $("#oddsOverUnder")
var $button = $("#roll")
var $bet = $("#bet")
var $pay = $("#oddsPayout.btn.btn-primary.btn-xlg.btn-block")
var inDynamicEditMode   = true; 

function basex() {
    $bet.val(start)
}

function timesx() {
    var lol = document.getElementById('bet').value; 
    $bet.val(lol * 8)
}

function paylowx() {
    $pay.click()
    document.getElementById("oddsInput").value = "1.2";
    $odds.click()
}

function payhighx() {
    $pay.click()
    document.getElementById("oddsInput").value = "5.2";
    $odds.click()
}

function payx(){

}

var mars = document.createElement('div');

mars.innerHTML = '' +
    '<div class="form-group">' +
    '<div class="text-center col-sm-6 col-sm-offset-3">' +
    '<input id="base" type="button" value="BASE" onClick="basex()">' +
    '<input id="times" type="button" value="x8" onClick="timesx()">' +
    '<input id="paylow" type="button" value="1.2" onClick="paylowx()">' +
    '<input id="payhigh" type="button" value="5.2" onClick="payhighx()">' +
    '</div>' +
    '</div>';
document.getElementsByClassName('text-center col-sm-6 col-sm-offset-3')[0].appendChild(mars);

var mybase = document.querySelector("#base");
mybase.style.backgroundColor = "#131313";
mybase.style.borderStyle = "none";
mybase.style.borderRadius = "6px 0px 0px 0px"
mybase.style.color = "#fff"
mybase.style.fontSize = "18px"
mybase.style.width = "80px"
mybase.style.height = "40px"

var mytimes = document.querySelector("#times");
mytimes.style.backgroundColor = "#131313";
mytimes.style.borderStyle = "none";
mytimes.style.borderRadius = "0px 6px 0px 0px"
mytimes.style.color = "#fff"
mytimes.style.fontSize = "18px"
mytimes.style.width = "80px"
mytimes.style.height = "40px"

var mypaylow = document.querySelector("#paylow");
mypaylow.style.backgroundColor = "#131313";
mypaylow.style.borderStyle = "none";
mypaylow.style.borderRadius = "0px 0px 0px 6px"
mypaylow.style.color = "#fff"
mypaylow.style.fontSize = "18px"
mypaylow.style.width = "80px"
mypaylow.style.height = "40px"

var mypayhigh = document.querySelector("#payhigh");
mypayhigh.style.backgroundColor = "#131313";
mypayhigh.style.borderStyle = "none";
mypayhigh.style.borderRadius = "0px 0px 6px 0px"
mypayhigh.style.color = "#fff"
mypayhigh.style.fontSize = "18px"
mypayhigh.style.width = "80px"
mypayhigh.style.height = "40px"

var myhalf = document.querySelector("#halfBetButton");
myhalf.style.display = "none"

var mydouble = document.querySelector("#doubleBetButton");
mydouble.style.display = "none"


function roll() {
    $bet.val(start)
    $button.click()
    setTimeout(function() {

        var tr = document.querySelector("#myBetsTable tr:nth-child(2)")
        var cls = tr.getAttribute('class')

        if (cls === 'success'){
            payx()
        }
        else{
            payx()
            inDynamicEditMode ^= true
        }
            $button.click();
    setTimeout(function() {
            $button.click();
            },1000);
            },1000);
}

setInterval(roll, 2000)

3 个答案:

答案 0 :(得分:1)

您可以将函数声明为变量。

// Therefore
payx = function() {
  // script
}
payx();

在您的代码中

if (inDynamicEditMode == 0) {
  payx = function() {
    $pay.click()
    document.getElementById("oddsInput").value = "1.2";
    $odds.click()
  }
}

if(inDynamicEditMode == 1){
  payx = function() {
    $pay.click()
    document.getElementById("oddsInput").value = "5.2";
    $odds.click()
  }
}

如果你问为什么要这样做,如果你经常调用这个函数就会有效。

答案 1 :(得分:1)

嗯,对于初学者,你可以定义一个inDynamicEditMode函数,它接受function payx(inDynamicEditMode) { if (inDynamicEditMode === 0) { // do stuff } if (inDynamicEditMode === 1) { // do some other stuff } } 参数作为参数,如下所示:

payx()

或者,您可以将函数表达式分配给// somewhere above var payx = function() { // do stuff, maybe default? }; if (inDynamicEditMode === 0) { payx = function() { // do stuff }; } if (inDynamicEditMode === 1) { payx = function() { // do other stuff }; } ,如下所示:

inDynamicEditMode

每次怀疑dataFile发生变化时,都需要调用上面的示例。

答案 2 :(得分:0)

为什么不使用一个函数(没有重新定义)并使用该函数中的三元运算符来确定要分配的值:

{% block choice_widget %}
    {%
    set labels = {
        1: { title: '1 ' ~ 'label.credits'|trans ~ ' - <strong>1€</strong>' },
        2: { title: '2 ' ~ 'label.credits'|trans ~ ' - <strong>2€</strong>' },
        25: { title: '25 ' ~ 'label.credits'|trans ~ ' - <strong>25€</strong>' },
        }
    %}

    <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {% if child.vars.value matches '/^[1|2|25]{1}/' %}
                {% set currentLabel = labels[child.vars.value].title %}
            {% else %}
                {% set currentLabel = '' %}
            {% endif %}
            {{ form_widget(child, {'label': currentLabel}) }}
            {{ form_label(child) }}
            {{ form_errors(child) }}
        {% endfor %}
    </div>
{% endblock %}