Javascript 检查高度是否已从一定量更改

Javascript 检查高度是否已从一定量更改,javascript,Javascript,我想检查一个数字是否从另一个数字增加或减少到某个数字 例如: Let a = 20; Let b = 20; 如果b比a增加了5,则返回true 如果b比a减少了5,则返回true 提前谢谢你的帮助 var lastHeightSize = document.body.scrollHeight; function checkBodySizeChange () { // instead of checking if lastHeightSize is not equal to doc

我想检查一个数字是否从另一个数字增加或减少到某个数字

例如:

Let a = 20;
Let b = 20; 
如果
b
a
增加了5,则返回
true

如果
b
a
减少了5,则返回
true

提前谢谢你的帮助

var lastHeightSize = document.body.scrollHeight;

function checkBodySizeChange () {
    // instead of checking if lastHeightSize is not equal to document.body.scrollheight,
    // check if document.body.scrollheight has increased or decreased by 20
    const heightChanged = lastHeightSize !== document.body.scrollHeight;
    if (heightChanged) {
        trigger(document.body, 'sizechange');
        lastHeightSize = document.body.scrollHeight;
    }
    window.requestAnimationFrame(checkBodySizeChange);
}
使用


然后你就可以看到它的正负变化量了

以下是反映rlemons答案的更新代码

  var lastHeightSize = document.body.scrollHeight;
  function checkBodySizeChange () {
    const heightChanged = Math.abs(lastHeightSize - document.body.scrollHeight) > 120;
    if (heightChanged) {
      trigger(document.body, 'sizechange');
      lastHeightSize = document.body.scrollHeight;
    }
    window.requestAnimationFrame(checkBodySizeChange);
  }

Math.abs(oldV-newV)>changedLimit
!非常感谢您的快速回复!我加上它作为答案。试图摆脱在评论中回答的习惯:P
  var lastHeightSize = document.body.scrollHeight;
  function checkBodySizeChange () {
    const heightChanged = Math.abs(lastHeightSize - document.body.scrollHeight) > 120;
    if (heightChanged) {
      trigger(document.body, 'sizechange');
      lastHeightSize = document.body.scrollHeight;
    }
    window.requestAnimationFrame(checkBodySizeChange);
  }