中断setInterval和变量undefined

时间:2016-02-05 02:56:29

标签: javascript

我是Javascript的新手,并尝试过这个脚本。点击它应该抓住物品的价格并停止降价。 Onclick,它没有停止,它没有抓住显示的价格。请指教。

价格应该是: -

起始价格:100 最低最高价格:90

function updateValue(task) {
  var amount = document.getElementById("amount").value + "00";
  var maxamt = document.getElementById("maxamount").value + "00";
  var amountgrabbed = document.getElementById("revese-amt").value;

  var decimal_places = 2;
  var decimal_factor = decimal_places === 0 ? 1 : Math.pow(10, decimal_places);
  var floored_number = Math.floor(amount) / decimal_factor;
  var floored_number_max = Math.floor(maxamt) / decimal_factor;
  floored_number = floored_number.toFixed(decimal_places);
  floored_number_max = floored_number_max.toFixed(decimal_places);

  if (task == "stop") {
    console.log(amountgrabbed);
    check(refreshid, "stop", amountgrabbed);
  } else {

    var refreshid = setInterval(function() {
      floored_number = floored_number - 1 / 100;
      floored_number = floored_number.toFixed(decimal_places);
      if (floored_number < floored_number_max) {
        check(refreshid, "over");
      } else {
        $('#revese-amt').html(floored_number);
      }

    }, 100);

  }

}

function check(refreshid, status, amt) {

  if (status == "over") {
    clearInterval(refreshid);
    $('#revese-amt').html('Deal Gone!');
  } else if (status == "stop") {
    console.log(amt);
    clearInterval(refreshid);
    alert('Congratulations! You have grabbed the item @ price of RM ' + amt);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input 
     type="text" 
     name="amount" 
     id="amount" 
     value="Start Price" 
     onclick=""/>
<BR>
<BR>
<input 
     type="text" 
     name="maxamount" 
     id="maxamount" 
     value="Lowest Max Price" 
     onblur="updateValue()"
 />
<span id="maxa"></span>
<BR>
<BR>
<div 
     style="width:130px; height:50px; background-color:red;"          
     onclick="updateValue('stop')">
  <center>MYR
    <p id="revese-amt"></p>
  </center>
</div>

2 个答案:

答案 0 :(得分:2)

您可以更改以下代码:

第一个html {"Code":null,"Msg":"."}代码,必须使用p方法来获取其文字。

textContent

答案 1 :(得分:0)

更改

 var amountgrabbed = document.getElementById("revese-amt").value;

 var amountgrabbed = $('#revese-amt').html();

这与您的

分配相匹配
 $('#revese-amt').html(floored_number);

然后在全局空间中声明var varid变量。随处可访问。

  $refreshid = setInterval(function() {
  floored_number = floored_number - 1 / 100;
  floored_number = floored_number.toFixed(decimal_places);
  if (floored_number < floored_number_max) {
    check(refreshid, "over");
  } else {
    $('#revese-amt').html(floored_number);
  }

}, 100);