我是新來的Javascript和嘗試做這個腳本。點擊它應該抓住物品的價格並停止價格下降。 Onclick,它不會停止,它不會顯示價格。請指教。打破setInterval和變量undefined
價格應該是: -
起始價格: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>
你只有經過兩個參數有3 –
@SpencerWieczorek功能我也通過3個參數, '如果(任務== 「停止」){ 的console.log(amountgrabbed); check(refreshid,「stop」,amountgrabbed); } ' – MuthaFury
@MuthaFury也許應該有一些東西在開始價格輸入的onclick值? –