search

Home  >  Q&A  >  body text

javascript - How to turn off the js effect of newly opened windows in js

I made a window shaking effect, but the newly opened window has a shaking effect, but how to clear the effect, and how to close it after clearing the effect, I don’t know how to write...

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>窗口抖动</title>
</head>
<body>
<script>
    var w=window.open('','', 'width=100,height=100');
    w.resizeTo(300,300);
    var loop;
    var timer;
    var offX;
    var offY;
    var status = 1;
    timer = setInterval(function(){
        w.moveTo(100,100);
        if(loop<10){
            clearInterval(timer);
        }
        status = Math.random()*10 > 5 ? 1 : -1;
        offX = Math.random()*20*status;
        offY = Math.random()*20*status*-1;
        w.moveBy(offX,offY);
        loop++;
    },10);
</script>
</body>
</html>
漂亮男人漂亮男人2787 days ago624

reply all(4)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:59:11

    var w=window.open('','', 'width=100,height=100');
    w.resizeTo(300,300);
    var loop = 0;  // 设置默认值
    var timer;
    var offX;
    var offY;
    var status = 1;
    timer = setInterval(function(){
        w.moveTo(100,100);
        // 设为大于
        if(loop > 10){
            clearInterval(timer);
        }
        status = Math.random()*10 > 5 ? 1 : -1;
        offX = Math.random()*20*status;
        offY = Math.random()*20*status*-1;
        w.moveBy(offX,offY);
        loop++;
    },10);
    w.close() // 关闭窗口

    reply
    0
  • 高洛峰

    高洛峰2017-05-18 10:59:11

    setTimeout(function () {
      clearInterval(timer);
    },1000);
    setTimeout(function () {
      w.close();
    },2000)

    reply
    0
  • 黄舟

    黄舟2017-05-18 10:59:11

    if(loop>10){
          clearInterval(timer);
    }

    The loop should be greater than 10. You wrote it wrong. Also give an initial value of zero when declaring the loop.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:59:11

    Clear the timer and then execute a method to close the window

    reply
    0
  • Cancelreply