简体   繁体   English

Setinterval似乎在1500毫秒而不是50毫秒上运行

[英]Setinterval seems to be running at 1500 ms instead of 50ms

I have a set interval function that seems to be running at 1500ms though it is set to 50ms. 我有一个设置的间隔函数,尽管将其设置为50ms,但它似乎以1500ms运行。 If you look at this script, it shrinks an element and shakes it a few degrees, I want it to run smoothly thought it is truly running at roughly ~1500ms. 如果您看一下此脚本,它会缩小一个元素并使其摇晃几度,我希望它能够平稳运行,因为它确实在大约1500ms左右运行。 Any help? 有什么帮助吗? Changing the setInterval form 1000 to 500 has little to no effect and that trend continues as your continue to halve the value. 将setInterval格式从1000更改为500几乎没有影响,并且这种趋势会随着您将值减半而继续。 I can't seem to find anything online on why this might be... 我似乎无法在网上找到任何有关这可能是为什么的信息...

   var rotated = false;
var height = 24.6;
var width = 15

var points = 0;
var cutvalw = 1;
var cutvalh = 1.64;
var val = 10;


document.querySelector("#box").addEventListener("click", function () {
  width = width - cutvalw;
  height = height - cutvalh;
  var div = document.getElementById("box"),
      deg = rotated ? 0 : 22;

  div.style.webkitTransform = "rotate("+deg+"deg)";
  div.style.mozTransform    = "rotate("+deg+"deg)";
  div.style.msTransform     = "rotate("+deg+"deg)";
  div.style.oTransform      = "rotate("+deg+"deg)";
  div.style.transform       = "rotate("+deg+"deg)";

  setTimeout(res, 140);
});
 function res() {
  var div = document.getElementById("box"),
      deg = rotated ? 0 : 0;
  div.style.webkitTransform = "rotate("+deg+"deg)";
  div.style.mozTransform    = "rotate("+deg+"deg)";
  div.style.msTransform     = "rotate("+deg+"deg)";
  div.style.oTransform      = "rotate("+deg+"deg)";
  div.style.transform       = "rotate("+deg+"deg)";
  }
setInterval(gamerule, 50);
function gamerule() {
   var div = document.getElementById("box");
  div.style.width           = width + "%";
  div.style.height          = height + "%";

  if (width < 1) {
    width = 15;
     height = 24.6;
    document.getElementById("cont").style.pointerEvents = "none";
    setInterval(ser, 1000);
    points++;
    function ser() {
         document.getElementById("cont").style.pointerEvents = "all";
    }
   }
}



setInterval(gamefunc, 500);

function gamefunc() {
  if (val >= 10) {
  width = width - .5;
  height = height - .5;
   var div = document.getElementById("box"),
      deg = rotated ? 0 : 22;

  div.style.webkitTransform = "rotate("+deg+"deg)";
  div.style.mozTransform    = "rotate("+deg+"deg)";
  div.style.msTransform     = "rotate("+deg+"deg)";
  div.style.oTransform      = "rotate("+deg+"deg)";
  div.style.transform       = "rotate("+deg+"deg)";

  setTimeout(res, 140);
};
  function res() {
 var div = document.getElementById("box"),
       deg = rotated ? 0 : 0;
  div.style.webkitTransform = "rotate("+deg+"deg)";
  div.style.mozTransform    = "rotate("+deg+"deg)";
   div.style.msTransform     = "rotate("+deg+"deg)";
  div.style.oTransform      = "rotate("+deg+"deg)";
  div.style.transform       = "rotate("+deg+"deg)";
 val = 9;
   setTimeout(game, 1000);
  function game() {
    val = 10;
  }
  }
}

You might have an issue with your curly braces, but I can't quite tell what you're trying to do. 您的大括号可能有问题,但是我不太清楚您要做什么。 The function "gameFunc" is actually creating a function called "res" on every execution, and only running the transformations if val is greater than or equal to 10. Naturally, creating a function doesn't actually execute the function, so that's probably why the interval doesn't appear to fire. 实际上,函数“ gameFunc”在每次执行时都会创建一个名为“ res”的函数,并且仅在val大于或等于10时才运行转换。间隔似乎没有触发。

Perhaps: 也许:

setInterval(gamefunc, 500);

function gamefunc() {
    if (val >= 10) {
       width = width - .5;
       height = height - .5;
       var div = document.getElementById("box"),
       deg = rotated ? 0 : 22;

       div.style.webkitTransform = "rotate("+deg+"deg)";
       div.style.mozTransform    = "rotate("+deg+"deg)";
       div.style.msTransform     = "rotate("+deg+"deg)";
       div.style.oTransform      = "rotate("+deg+"deg)";
       div.style.transform       = "rotate("+deg+"deg)";
   }
   setTimeout(res, 140);
};
function res() {
    var div = document.getElementById("box"),
    deg = rotated ? 0 : 0;
    div.style.webkitTransform = "rotate("+deg+"deg)";
    div.style.mozTransform    = "rotate("+deg+"deg)";
    div.style.msTransform     = "rotate("+deg+"deg)";
    div.style.oTransform      = "rotate("+deg+"deg)";
    div.style.transform       = "rotate("+deg+"deg)";
    val = 9;
    setTimeout(game, 1000);
}
function game() {
    val = 10;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 setInterval 间隔从 100 毫秒更改为 50 毫秒 - change setInterval interval from 100ms to 50ms 节点js,为什么setTimeout用50ms比setTimeout 0快 - Node js, why is setTimeout with 50ms is faster than setTimeout 0 为什么AJAX请求所需的时间从2ms跳到50ms甚至更高? - Why does the time taken for AJAX request jump from 2ms to 50ms and higher? 快速(每50ms左右)用图像更新材质的纹理贴图会导致严重的卡顿 - Updating a material's texture map with images rapidly (every 50ms or so) causes huge stuttering 我试图让 JavaScript function 在特定时间间隔内每 50 毫秒运行一次,但它不起作用 - I am trying to make a JavaScript function run every 50ms for a certain time interval, but it's not working 为什么JS .focus()只能在50ms或更长的setTimeout内工作 - Why does JS .focus() only work inside a setTimeout of 50ms or longer 移相器-显示图形对象50毫秒,并在同时发生一次或多次按键时每次按键破坏 - Phaser - Show graphic objects for 50ms and destroy for each key press when one or more key presses occur simultaneously 在用户滚动到它们之前触发所有 `content-visibility: auto` 元素的渲染,而不会阻塞主线程超过 50 毫秒 - Trigger rendering of all `content-visibility: auto` elements BEFORE a user scrolls to them without blocking main thread for >50ms Chrome setInterval在10000毫秒时崩溃 - Chrome setInterval crashes at 10000 ms 根据条件更改setInterval的MS - Changing MS of setInterval depending on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM