gpt4 book ai didi

jquery - 根据生成的随机数重复更改颜色

转载 作者:行者123 更新时间:2023-11-28 05:17:04 26 4
gpt4 key购买 nike

因此此页面每 4 秒生成一个 0 到 180 之间的随机数。该数字分配给 changingSpeed。我们试图根据每次更改时的随机数一遍又一遍地更改背景颜色。它几乎可以工作.. 但条件只触发 else,而不是 ifelse if

<code>
<script>
/* I set the interval to 4 seconds 4000ms */
var id = window.setInterval(function(){randomNumber();},4000);

function randomNumber()
{

var rand = Math.floor(Math.random()*180);
//Do whatever you want with that number
$('#changingSpeed').html(rand);


if (changingSpeed <= 60) {
$('#meter_box').css('background-color', 'rgba(0, 255, 0, 0.2 )');
} else if (changingSpeed <= 120) {
$('#meter_box').css('background-color', 'rgba(0, 0, 255, 0.2 )');
} else {
$('#meter_box').css('background-color', 'rgba(255, 0, 0, 0.2 )');
}


}

</script>


<script src="http://static.jsbin.com/js/render/edit.js?3.39.14"></script>
<script>jsbinShowEdit && jsbinShowEdit ({"static":"http://static.jsbin.com","root":"http://jsbin.com"});</script>
<script>


/* Generate a random number */
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore (a,m)
})(window,document,'script','https://www.google- analytics.com/analytics.js','ga');

ga('create', 'UA-1656750-34', 'auto');
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');

</script>

最佳答案

效果很好这是一个jsFiddle

<html>
<head><script src="https://code.jquery.com/jquery-3.1.0.min.js"></script></head><body>

<div id=meter_box style="width:50px; height:50px;"></div>
<script>
/* I set the interval to 4 seconds 4000ms */
var id = window.setInterval(function(){randomNumber();},4000);

function randomNumber()
{

var rand = Math.floor(Math.random()*180);
// define the changingSpeed var as rand;
var changingSpeed = rand;


if (changingSpeed <= 60) {
$('#meter_box').css('background-color', 'rgba(0, 255, 0, 0.2 )');
} else if (changingSpeed <= 120) {
$('#meter_box').css('background-color', 'rgba(0, 0, 255, 0.2 )');
} else {
$('#meter_box').css('background-color', 'rgba(255, 0, 0, 0.2 )');
}


}

</script></body></html>

关于jquery - 根据生成的随机数重复更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39260849/

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