web js中是否存在等待或睡眠命令

Is there a wait or sleep command in web js?

本文关键字:命令 在等待 存在 js 是否 web      更新时间:2023-09-26

我正在制作背景变化缓慢但颜色变化较快的东西,所以我想知道是否有webjs 的睡眠命令

如果您想更改setInterval,可以使用它:

var colors = ['red', 'green', 'blue']
var count = 0;

setInterval(function () {
  document.body.style.backgroundColor = colors[count];
  count ++ ;
  if(count >= colors.length){
      count = 0;
  }
},1000)

https://jsfiddle.net/kemiller2002/od5ho3f2/

如果您试图只为背景制作动画或转换,请阅读css中的转换,并使用css进行转换,以提高性能和简洁性。

https://www.google.bg/search?q=css+过渡+变化+背景+图像&oq=css+过渡+变化+背景+图像&aqs=铬。。69i57j0l2.261j0j1&sourceid=chrome&ie=UTF-8

CSS动画是一个不错的方法

@keyframes example {
    from {background-color: red;}
    to {background-color: purple;}
}

div {
    width: 100px;
    height: 100px;
    background-color: red;
    /* Reference her here */
    animation-name: example;
    animation-duration: 4s;
}

http://www.w3schools.com/css/css3_animations.asp