使用Jquery延迟满足条件

时间:2018-01-19 18:27:34

标签: javascript jquery

我正在尝试创建一个带有碰撞检测的动画,当满足条件时,它会等待大约500毫秒来触发下一个条件。我尝试过.delay()方法,但它似乎不起作用。我正在为其他部分使用jQuery和jQuery API。

if (r1x>=p1x && r1x<=p1y){
    planta1.style.width="50px";
    planta1.style.top="-90px"
    planta1.style.left="30px"
    planta1.src="imagens/jogo_1/planta1_2.png";
    planta1nmr=1;

    if (planta1nmr==1){
      planta1.style.width="100px";
      planta1.style.top="-100px"
      planta1.style.left="10px"
      planta1.src="imagens/jogo_1/planta1_3.png";
      planta1nmr=2;

      if(planta1nmr==2){
        planta1.style.width="100px";
        planta1.style.top="-110px"
        planta1.style.left="10px"
        planta1.src="imagens/jogo_1/planta1_4.png";
        planta1nmr=3;

        if (planta1nmr==3){
          planta1.style.width="100px";
          planta1.style.top="-120px"
          planta1.style.left="10px"
          planta1.src="imagens/jogo_1/planta1_5.png";
          coracao1.style.visibility="visible";
          coracao1.style.top="500px";
          coracao1.style.left="95px";
          pontosjogo1facil++;
        }
      }
    }
  }

2 个答案:

答案 0 :(得分:0)

您可以.queue().delay()一起使用,通过将相同的queueName传递给两种方法来实现要求

&#13;
&#13;
$({})
.delay(500, "delay")
.queue("delay", function() {
  console.log("do stuff")
}).dequeue("delay");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我会捅这个。

使用超时。

https://www.w3schools.com/jsref/met_win_settimeout.asp

if (r1x>=p1x && r1x<=p1y){
    planta1.style.width="50px";
    planta1.style.top="-90px"
    planta1.style.left="30px"
    planta1.src="imagens/jogo_1/planta1_2.png";

    setTimeout(function() {
        planta1.style.width="100px";
        planta1.style.top="-100px"
        planta1.style.left="10px"
        planta1.src="imagens/jogo_1/planta1_3.png";

        setTimeout(function() {
            planta1.style.width="100px";
            planta1.style.top="-110px"
            planta1.style.left="10px"
            planta1.src="imagens/jogo_1/planta1_4.png";

            setTimeout(function() {
                planta1.style.width="100px";
                planta1.style.top="-120px"
                planta1.style.left="10px"
                planta1.src="imagens/jogo_1/planta1_5.png";
                coracao1.style.visibility="visible";
                coracao1.style.top="500px";
                coracao1.style.left="95px";
                pontosjogo1facil++;
            }, 500); // end last test

        }, 500); //end second test

    }, 500); // end first test
}

但我同意一些意见,我不确定你要做什么。