Why is the background color blinking instead of fading?

In this small code, I try to change the background color of the body smoothly, since the first test he did the best job at a time, in another test the color changed with a blink. Who knows the reason, requests allow this for me? It was against my goal for this program, but I found that it was real color dancing.: D

<html>
<body>
 Interval:<input type="number" id="interval" min="0" placeholder="millisecond" value="10" step="10"/>
 Start Point Color: 
  R<input type="number" id="redStart" min="0" max="255" placeholder="ed" value="0"/>
  G<input type="number" id="greenStart" min="0" max="255" placeholder="reen" value="0"/> 
  B<input type="number" id="blueStart" min="0" max="255" placeholder="lue" value="0"/>
 Dancing Color:
  R:<input type="checkbox" id="redDance" checked="true"/>
  G:<input type="checkbox" id="greenDance"/>  
  B:<input type="checkbox" id="blueDance"/>
  <button type="button" id="dance">Dance</button>
  <button type="button" id="stopDance">Stop Dance</button>

 <script type="text/javascript">

	//event for click on dance button
    document.getElementById('dance').onclick = Dancer;
	document.getElementById('stopDance').onclick = stopDancer;

	// this is the dancer function that is responsible for the background color dancing
	function Dancer(){
		var body_color  = document.body.style.backgroundColor;
		var interval_ms = document.getElementById('interval').value;
		var is_red = document.getElementById('redDance').checked;
		var is_green = document.getElementById('greenDance').checked;
		var is_blue = document.getElementById('blueDance').checked;
		var red = document.getElementById('redStart').value;
		var green = document.getElementById('greenStart').value;
		var blue = document.getElementById('blueStart').value;
		//document.body.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
	    document.dancing = setInterval(function(){
			if (is_red && red < 255){
			 red++;
			 document.getElementById('redStart').value = red;
			}
			if(is_green && green < 255){
			 green++;
			 document.getElementById('greenStart').value = green;
			}
			if(is_blue && blue < 255){
			 blue++;
			 document.getElementById('blueStart').value = blue;
			}
			document.body.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
		},interval_ms
		);
	}
	
	//this is the dancer stop function
	function stopDancer(){
		clearInterval(document.dancing);
	}
  
 </script>
 </body>
 
</html>
Run code
+4
source share
2 answers

Try Fiddle .

I just added clearIntervalat the beginning of your function. Now it does not blink. Is that what you wanted?

+2
source

, Dancer() . else, , if, , . else if, .

//event for click on dance button
document.getElementById('dance').onclick = Dancer;
document.getElementById('stopDance').onclick = stopDancer;

// this is the dancer function that is responsible for the background color dancing
function Dancer(){
  clearInterval(document.dancing);
  var body_color  = document.body.style.backgroundColor;
  var interval_ms = document.getElementById('interval').value;
  var is_red = document.getElementById('redDance').checked;
  var is_green = document.getElementById('greenDance').checked;
  var is_blue = document.getElementById('blueDance').checked;
  var red = document.getElementById('redStart').value;
  var green = document.getElementById('greenStart').value;
  var blue = document.getElementById('blueStart').value;
  var redDone = false;
  var greenDone = false;
  var blueDone = false;
  document.dancing = setInterval(function(){
    if (is_red && red < 255){
      red++;
      document.getElementById('redStart').value = red;
    }
    else if (!is_red && red > 0){
      red--;
      document.getElementById('redStart').value = red;
    }
    else {
      redDone = true;
    }
    if(is_green && green < 255){
      green++;
      document.getElementById('greenStart').value = green;
    }
    else if(!is_green && green > 0){
      green--;
      document.getElementById('greenStart').value = green;
    }
    else {
      greenDone = true;
    }
    if(is_blue && blue < 255){
      blue++;
      document.getElementById('blueStart').value = blue;
    }
    else if(!is_blue && blue > 0){
      blue--;
      document.getElementById('blueStart').value = blue;
    }
    else {
      blueDone = true;
    }
    document.body.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
    if(redDone && greenDone && blueDone){
      clearInterval(document.dancing);
    }
  },interval_ms
                                );
}

//this is the dancer stop function
function stopDancer(){
  clearInterval(document.dancing);
}
Interval:<input type="number" id="interval" min="0" placeholder="millisecond" value="10" step="10"/>
Start Point Color: 
R<input type="number" id="redStart" min="0" max="255" placeholder="ed" value="0"/>
G<input type="number" id="greenStart" min="0" max="255" placeholder="reen" value="0"/> 
B<input type="number" id="blueStart" min="0" max="255" placeholder="lue" value="0"/>
Dancing Color:
R:<input type="checkbox" id="redDance" checked="true"/>
G:<input type="checkbox" id="greenDance"/>  
B:<input type="checkbox" id="blueDance"/>
<button type="button" id="dance">Dance</button>
<button type="button" id="stopDance">Stop Dance</button>
+2

Source: https://habr.com/ru/post/1610124/


All Articles