如何正确使用变量

时间:2016-11-14 15:09:41

标签: javascript

我有这个代码从提供的HTML中获取数据,然后在颜色上下注。但每次function compare的输出为lose时,其余代码都会失败并输出NaN

var green_id = document.getElementsByClassName("btn btn-success btn-lg btn-block betButton")[0];
var red_id = document.getElementsByClassName("btn btn-danger btn-lg btn-block betButton")[0];
var black_id = document.getElementsByClassName("btn btn-inverse btn-lg btn-block betButton")[0];
var green_button = document.getElementById('green');

document.getElementById('betAmount').value = '';
red_id.id = 'red';
green_id.id = 'green';
black_id.id = 'black';

setInterval(automated, 40000);

function automated() {

  var roll_hisLast = $('#past')[0].childNodes[8].textContent;
  var roll_hisThis = $('#past')[0].childNodes[9].textContent;
  var base_bet = 1;
  var red_button = document.getElementById('red');
  var black_button = document.getElementById('black');

  function ThisRoll(roll_hisThis) {
    var this_roll;
    if (roll_hisThis === 0) {
      this_roll = 'green';
    } else if ((roll_hisThis >= 1) && (roll_hisThis <= 7)) {
      this_roll = 'red';
    } else if ((roll_hisThis >= 8) && (roll_hisThis <= 14)) {
      this_roll = 'black';
    }
    return this_roll;
  }
  var thisRoll = ThisRoll(roll_hisThis);
  console.log(ThisRoll);

  function LastRoll(roll_hisLast) {

    var last_roll;
    if (roll_hisLast === 0) {
      last_roll = 'green';
    } else if ((roll_hisLast >= 1) && (roll_hisLast <= 7)) {
      last_roll = 'red';
    } else if ((roll_hisLast >= 8) && (roll_hisLast <= 14)) {
      last_roll = 'black';
    }
    return last_roll;
  }
  var lastRoll = LastRoll(roll_hisLast);
  console.log(LastRoll);

  function compare(thisRoll, lastRoll) {
    var win_lose;
    if (thisRoll !== lastRoll) {
      win_lose = 'lose';
    } else {
      win_lose = 'win';
    }
    return win_lose;
  }
  var winLose = compare(thisRoll, lastRoll);
  console.log(winLose);

  function betInput(winLose) {

    var x;
    if (winLose === 'win') {
      x = base_bet;
    } else if (winLose === 'lose') {
      x = lastX * 2;
    }
    return x;
  }
  var X = betInput(winLose);
  console.log(X);

  function betInputLast(X) {

    var last;
    last = X;
    return last;
  }
  var lastX = betInputLast(X);
  console.log(lastX);

  var bet_input = document.getElementById('betAmount').value = X;

  function bet(thisRoll, X) {

    var log;
    if (thisRoll === 'red') {
      red_button.click();
      log = 'Betting ' + X + ' on red';
    } else if (thisRoll === 'black') {
      black_button.click();
      log = 'Betiing ' + X + ' on black';
    }
    return log;
  }
  var Log = bet(thisRoll, X);
  console.log(Log);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="roulette">
  <div class="well text-center" style="margin-bottom:10px;margin-top:25px; padding: 20px;">
    <div class="progress text-center" style="height:50px;margin-bottom:10px;">
      <span id="banner">Rolling in 3.88...</span>
      <div class="progress-bar progress-bar-danger" id="counter" style="width: 97.11070183887917%; overflow: hidden;"></div>
    </div>
    <div id="case" style="margin-bottom:15px">
      <div id="pointer"></div>
    </div>
    <div id="past">
      <div data-rollid="692610" class="ball ball-0">0</div>
      <div data-rollid="692611" class="ball ball-8">9</div>
      <div data-rollid="692612" class="ball ball-8">9</div>
      <div data-rollid="692613" class="ball ball-1">3</div>
      <div data-rollid="692614" class="ball ball-8">13</div>
      <div data-rollid="692615" class="ball ball-1">5</div>
      <div data-rollid="692616" class="ball ball-1">2</div>
      <div data-rollid="692617" class="ball ball-1">3</div>
      <div data-rollid="692618" class="ball ball-8">13</div>
      <div data-rollid="692619" class="ball ball-8">11</div>
    </div>
    <div style="margin: 20px 0px;"></div>
    <div class="form-group">
      <div class="input-btn bet-buttons">
        <span class="balance"> 
                <span>Balance: </span>
        <span id="dongers_r"></span>
        <span id="balance_r">12022</span>
        <i style="cursor:pointer; margin-left: 5px;" class="fa fa-refresh noselect" id="getbal_r"></i>
        </span>
        <input type="text" class="form-control input-lg" placeholder="Bet amount..." id="betAmount">
        <button type="button" class="btn btn-danger betshort" data-action="clear">Clear</button>
        <button type="button" class="btn btn-default betshort" data-action="10">+10</button>
        <button type="button" class="btn btn-default betshort" data-action="100">+100</button>
        <button type="button" class="btn btn-default betshort" data-action="1000">+1000</button>
        <button type="button" class="btn btn-default betshort" data-action="half">1/2</button>
        <button type="button" class="btn btn-default betshort" data-action="double">x2</button>
        <button type="button" class="btn btn-primary betshort" data-action="max">Max</button>
      </div>
    </div>
  </div>
  <div class="row text-center">
    <div class="col-xs-4 betBlock" style="padding-right:0px">
      <div class="panel panel-default bet-panel" id="panel11-7-b">
        <div class="panel-heading">
          <button class="btn btn-danger btn-lg  btn-block betButton" data-lower="1" data-upper="7"><span> 1 to 7</span><span></span>
          </button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel1-7-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel1-7-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
    <div class="col-xs-4 betBlock">
      <div class="panel panel-default bet-panel" id="panel0-0-b">
        <div class="panel-heading">
          <button class="btn btn-success btn-lg  btn-block betButton" data-lower="0" data-upper="0">0</button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel0-0-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel0-0-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
    <div class="col-xs-4 betBlock" style="padding-left:0px">
      <div class="panel panel-default bet-panel" id="panel8-14-b">
        <div class="panel-heading">
          <button class="btn btn-inverse btn-lg  btn-block betButton" data-lower="8" data-upper="14"><span> 8 to 14</span><span></span>
          </button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel8-14-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel8-14-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

这个在加载或运行时没有错误。请查看是否有帮助

&#13;
&#13;
var green_id = $(".btn-success");
var red_id = $(".btn-danger").attr("id","red");;
var black_id = $(".btn-inverse").attr("id","black");;
var green_button = $('#green').attr("id","green");;
var red_button = $('#red');
var black_button = $('#black');
var roll_time = parseFloat($('#banner>span').text());
console.log("roll_time",roll_time);
$('#betAmount').val("");
automated();
setInterval(automated, 40000);

function automated() {

  var roll_hisLast = parseInt($('#past').children().eq(8).text(),10);
  var roll_hisThis = parseInt($('#past').children().eq(9).text(),10);
  var base_bet = 1;

  function ThisRoll(roll_hisThis) {
    console.log("roll_hisThis",roll_hisThis);
    var this_roll;
    if (roll_hisThis === 0) {
      this_roll = 'green';
    } else if ((roll_hisThis >= 1) && (roll_hisThis <= 7)) {
      this_roll = 'red';
    } else if ((roll_hisThis >= 8) && (roll_hisThis <= 14)) {
      this_roll = 'black';
    }
    return this_roll;
  }
  var thisRoll = ThisRoll(roll_hisThis);
  console.log("thisRoll",thisRoll); // lowercased the T

  function LastRoll(roll_hisLast) {

    var last_roll;
    if (roll_hisLast === 0) {
      last_roll = 'green';
    } else if ((roll_hisLast >= 1) && (roll_hisLast <= 7)) {
      last_roll = 'red';
    } else if ((roll_hisLast >= 8) && (roll_hisLast <= 14)) {
      last_roll = 'black';
    }
    return last_roll;
  }
  var lastRoll = LastRoll(roll_hisLast);
  console.log(lastRoll); // lowercased the L

  function compare(thisRoll, lastRoll) {
    var win_lose;
    if (thisRoll !== lastRoll) {
      win_lose = 'lose';
    } else {
      win_lose = 'win';
    }
    return win_lose;
  }
  var winLose = compare(thisRoll, lastRoll);
  console.log(winLose);

  function betInput(winLose) {

    var x;
    if (winLose === 'win') {
      x = base_bet;
    } else if (winLose === 'lose') {
      x = lastX * 2;
    }
    return x;
  }
  var X = betInput(winLose);
  console.log(X);

  function betInputLast(X) {

    var last;
    last = X;
    return last;
  }
  var lastX = betInputLast(X);
  console.log(lastX);

  var bet_input = $('#betAmount').value = X;

  function bet(thisRoll, X) {

    var log;
    if (thisRoll === 'red') {
      red_button.click();
      log = 'Betting ' + X + ' on red';
    } else if (thisRoll === 'black') {
      black_button.click();
      log = 'Betting ' + X + ' on black';
    }
    return log;
  }
  var Log = bet(thisRoll, X);
  console.log(Log);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="roulette">
  <div class="well text-center" style="margin-bottom:10px;margin-top:25px; padding: 20px;">
    <div class="progress text-center" style="height:50px;margin-bottom:10px;">
      <div id="banner">Rolling in <span>3.88</span>...</span>
      <div class="progress-bar progress-bar-danger" id="counter" style="width: 97.11070183887917%; overflow: hidden;"></div>
    </div>
    <div id="case" style="margin-bottom:15px">
      <div id="pointer"></div>
    </div>
    <div id="past">
      <div data-rollid="692610" class="ball ball-0">0</div>
      <div data-rollid="692611" class="ball ball-8">9</div>
      <div data-rollid="692612" class="ball ball-8">9</div>
      <div data-rollid="692613" class="ball ball-1">3</div>
      <div data-rollid="692614" class="ball ball-8">13</div>
      <div data-rollid="692615" class="ball ball-1">5</div>
      <div data-rollid="692616" class="ball ball-1">2</div>
      <div data-rollid="692617" class="ball ball-1">3</div>
      <div data-rollid="692618" class="ball ball-8">13</div>
      <div data-rollid="692619" class="ball ball-8">11</div>
    </div>
    <div style="margin: 20px 0px;"></div>
    <div class="form-group">
      <div class="input-btn bet-buttons">
        <span class="balance"> 
                <span>Balance: </span>
        <span id="dongers_r"></span>
        <span id="balance_r">12022</span>
        <i style="cursor:pointer; margin-left: 5px;" class="fa fa-refresh noselect" id="getbal_r"></i>
        </span>
        <input type="text" class="form-control input-lg" placeholder="Bet amount..." id="betAmount">
        <button type="button" class="btn btn-danger betshort" data-action="clear">Clear</button>
        <button type="button" class="btn btn-default betshort" data-action="10">+10</button>
        <button type="button" class="btn btn-default betshort" data-action="100">+100</button>
        <button type="button" class="btn btn-default betshort" data-action="1000">+1000</button>
        <button type="button" class="btn btn-default betshort" data-action="half">1/2</button>
        <button type="button" class="btn btn-default betshort" data-action="double">x2</button>
        <button type="button" class="btn btn-primary betshort" data-action="max">Max</button>
      </div>
    </div>
  </div>
  <div class="row text-center">
    <div class="col-xs-4 betBlock" style="padding-right:0px">
      <div class="panel panel-default bet-panel" id="panel11-7-b">
        <div class="panel-heading">
          <button class="btn btn-danger btn-lg  btn-block betButton" data-lower="1" data-upper="7"><span> 1 to 7</span><span></span>
          </button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel1-7-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel1-7-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
    <div class="col-xs-4 betBlock">
      <div class="panel panel-default bet-panel" id="panel0-0-b">
        <div class="panel-heading">
          <button class="btn btn-success btn-lg  btn-block betButton" data-lower="0" data-upper="0">0</button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel0-0-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel0-0-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
    <div class="col-xs-4 betBlock" style="padding-left:0px">
      <div class="panel panel-default bet-panel" id="panel8-14-b">
        <div class="panel-heading">
          <button class="btn btn-inverse btn-lg  btn-block betButton" data-lower="8" data-upper="14"><span> 8 to 14</span><span></span>
          </button>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel8-14-m">
        <div class="panel-body" style="padding:0px">
          <div class="my-row">
            <div class="text-center">
              <span class="mytotal">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="panel panel-default bet-panel" id="panel8-14-t">
        <div class="panel-body" style="padding:0px">
          <div class="total-row">
            <div class="text-center">Total bet: <span class="total">0</span>
            </div>
          </div>
          <ul class="list-group betlist"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
</div>
&#13;
&#13;
&#13;