gpt4 book ai didi

javascript - 中断 setInterval 和变量未定义

转载 作者:行者123 更新时间:2023-11-30 16:17:52 24 4
gpt4 key购买 nike

我是 Javascript 的新手并尝试执行此脚本。单击它应该获取项目的价格并停止价格下降。单击时,它不会停止,也不会获取显示的价格。请指教。

价格应该是:-

起始价格: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>

最佳答案

您可以更改以下代码:

第一个 html p 标签,必须使用 textContent 方法来获取它的文本。

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

// to
var amountgrabbed = document.getElementById("revese-amt").textContent;

关于javascript - 中断 setInterval 和变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215546/

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