无法通过计数计时器获取IF语句在Javascript中工作

问题描述 投票:0回答:1

我想让这个IF语句有效。我今天花了好几个小时挣扎。这里的想法是,我的程序将选择一个随机数。 (1到5之间)有一个从零开始的计数器,当它到达这个随机数时,它会提醒我一个文本描述。我也试图让它播放声音(只是一个方波),但现在我已经注释掉了,因为当计时器到达随机数时我甚至无法显示它。

<!DOCTYPE html>
<html>
<body>

<h2>Javascript CART breakdown calculator</h2>

<button onclick="pause_function()">pause</button>
<button onclick="resume_speed_function()">resume</button>


<label id="seconds">0</label>
<p id="random_number_display"></p>
<p id="breakdown_item"></p>


<script>

var my_random_number, low, high;
low = 1;
high = 5;
my_random_number = Math.floor(Math.random() * high) + low;
document.getElementById("random_number_display").innerHTML = my_random_number;

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();

var random_breakdown_event, low_event, high_event;
low_event = 1
high_event = 9
random_breakdown_event = Math.floor(Math.random() * high_event) + low_event;
var text

if (random_breakdown_event == 1) text = "blown engine";
if (random_breakdown_event == 2) text = "broken transmission";
if (random_breakdown_event == 3) text = "broken suspension";
if (random_breakdown_event == 4) text = "broken halfshaft";
if (random_breakdown_event == 5) text = "broken cv joint";
if (random_breakdown_event == 6) text = "broken wings";
if (random_breakdown_event == 7) text = "electrical misfire";
if (random_breakdown_event == 8) text = "broken fuel pump";
if (random_breakdown_event == 9) text = "change battery";

var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
var interval_between_ticks = 1000;
//Assign the interval to a variable to keep track of it
var interval = setInterval(setTime, interval_between_ticks);

function setTime() {
++totalSeconds;
secondsLabel.innerHTML = totalSeconds;
}

function  pause_function() {
interval_between_ticks = 999999999;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}

function  resume_speed_function() {
interval_between_ticks = 1000;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}


if(my_random_number==totalSeconds){
document.getElementById("breakdown_item").innerHTML = text}

//if(my_random_number>=4){
//function myFunction(frequency, duration, callback) {
//    duration = 10000 / 1000;     // the 10000 used to be 'duration' 
//    oscillator.type = 'square';
//    oscillator.frequency.value = 500; // value in hertz
//   oscillator.connect(audioCtx.destination);
//    oscillator.onended = callback;
//    oscillator.start(0);
//    oscillator.stop(audioCtx.currentTime + duration);
//}


</script>


</body>
</html>
javascript random timer
1个回答
0
投票

您必须在setTime函数中移动if块。

在原始实现中,my_random_number和totalSeconds之间的相等性检查仅在脚本执行时第一次执行。但它应该在每次增加秒数(即setTime函数)时检查相等性。下面的代码应该有效。

<!DOCTYPE html>
<html>
<body>

<h2>Javascript CART breakdown calculator</h2>

<button onclick="pause_function()">pause</button>
<button onclick="resume_speed_function()">resume</button>


<label id="seconds">0</label>
<p id="random_number_display"></p>
<p id="breakdown_item"></p>


<script>

var my_random_number, low, high;
low = 1;
high = 5;
my_random_number = Math.floor(Math.random() * high) + low;
document.getElementById("random_number_display").innerHTML = my_random_number;

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();

var random_breakdown_event, low_event, high_event;
low_event = 1
high_event = 9
random_breakdown_event = Math.floor(Math.random() * high_event) + low_event;
var text

if (random_breakdown_event == 1) text = "blown engine";
if (random_breakdown_event == 2) text = "broken transmission";
if (random_breakdown_event == 3) text = "broken suspension";
if (random_breakdown_event == 4) text = "broken halfshaft";
if (random_breakdown_event == 5) text = "broken cv joint";
if (random_breakdown_event == 6) text = "broken wings";
if (random_breakdown_event == 7) text = "electrical misfire";
if (random_breakdown_event == 8) text = "broken fuel pump";
if (random_breakdown_event == 9) text = "change battery";

var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
var interval_between_ticks = 1000;
//Assign the interval to a variable to keep track of it
var interval = setInterval(setTime, interval_between_ticks);

function setTime() {
  ++totalSeconds;
  secondsLabel.innerHTML = totalSeconds;
  if (my_random_number == totalSeconds) {
    document.getElementById("breakdown_item").innerHTML = text;
  }
}

function  pause_function() {
interval_between_ticks = 999999999;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}

function  resume_speed_function() {
interval_between_ticks = 1000;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}

//if(my_random_number>=4){
//function myFunction(frequency, duration, callback) {
//    duration = 10000 / 1000;     // the 10000 used to be 'duration' 
//    oscillator.type = 'square';
//    oscillator.frequency.value = 500; // value in hertz
//   oscillator.connect(audioCtx.destination);
//    oscillator.onended = callback;
//    oscillator.start(0);
//    oscillator.stop(audioCtx.currentTime + duration);
//}


</script>


</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.