我想創建一個函數,像一個泵,所以它只會執行代碼,而一個按鈕有一個'開'值。我已經做了以下但這恰恰被鎖定在一個循環中,可能有人幫助,謝謝只做一些代碼,而一個按鈕有一個特定的值屬性
<div id="main">
<div id="intruc" class="instructions">Instructions go in here</div>
<input type="button" value="On" id="onoff" onclick="pump();">
<div id="moniac"></div>
</div>
<script>
function pump(button) {
console.log("pump");
currentvalue = document.getElementById('onoff').value;
if(currentvalue == "Off"){
document.getElementById("onoff").value="On";
currentvalue == "On"
run(currentvalue)
}
else{
document.getElementById("onoff").value="Off";
currentvalue == "Off"
run(currentvalue)
}
}
function run(status) {
console.log("run");
console.log(status);
if (status==="On"){
console.log("do all the code")
run(status)
}
}
</script>
您是否在尋找mousedown事件? https://developer.mozilla.org/en/docs/Web/Events/mousedown –
我認爲你不應該使用'currentvalue == Off'或'on'而不是'currentvalue = Off'或'on' .. ..也是你的最後一個運行函數不需要 – Mostafa