简体   繁体   English

JavaScript 在 setInterval 调用的函数中使用 screen.width 或 screen.availWidth 时未获取更新值

[英]JavaScript Not getting an updated value when using screen.width or screen.availWidth in a function which is called by setInterval

Expected behavior:预期行为:

  1. setInterval calls getScreenWidth() every 2 sec. setInterval每 2 秒调用getScreenWidth()
  2. getScreenWidth() gets the current width of the (browser) screen. getScreenWidth()获取(浏览器)屏幕的当前宽度。
  3. Width is logged into console.宽度登录到控制台。
  4. We change the screen width.我们改变屏幕宽度。
  5. We see an update in the console output.我们在控制台输出中看到了更新。

Output:输出:

"1536"

The output is always a constant value.输出始终是一个恒定值。 (in my case 1536) (在我的情况下是 1536)

I was trying this code on CodePen, and I've tried:我正在 CodePen 上尝试此代码,并且尝试过:

  1. Resizing the output window.调整输出窗口的大小。 (changing it's width) (改变它的宽度)
  2. Resizing the browser.调整浏览器大小。 (changing width and even height) (改变宽度甚至高度)

The HTML: HTML:

<div id="demo"></div>

The script:剧本:

"use strict";
console.clear();

function getScreenWidth() {
    return screen.availWidth;
    // I've also tried screen.width
}

const demo = document.getElementById("demo");
let count = 0;

const intervalId = setInterval(function () {
    demo.innerHTML = getScreenWidth();
    console.clear();
    console.log(demo.innerHTML);
    console.log(count);
    count++;

    if (count > 30) {
        clearInterval(intervalId);
    }
}, 2000);

Since setInterval is indirectly calling getScreenWidth() every 2 sec, shouldn't screen.width be a fresh value every time?由于setInterval每 2 秒间接调用getScreenWidth() ,因此screen.width不应该每次都是一个新值吗?

If I assume that it's a fresh value, then does that mean screen.width doesn't equal the current width of the browser window?如果我假设它是一个新值,那么这是否意味着screen.width不等于浏览器窗口的当前宽度?

Note: The count variable in the code is just to stop setInterval from running indefinitely in the background.注意:代码中的count变量只是为了阻止setInterval在后台无限期运行。

I have the same screen width :)我有相同的屏幕宽度:)

Anyway, you don't need setInterval you can change it every time the browser is resized using the window onresize event:无论如何,您不需要setInterval您可以在每次使用 window onresize事件调整浏览器大小时更改它:

window.onresize = function() {
  screen.width; // 1536 / 1530 etc.
};

You also cannot get the browser width (with screen.width )您也无法获得浏览器宽度(使用screen.width

But you can easily get it using jQuery's .width() function:但是您可以使用 jQuery 的.width()函数轻松获取它:

$(window).width();

Here's a working example (using jQuery):这是一个工作示例(使用 jQuery):

 "use strict"; console.clear(); function getScreenWidth() { return $(window).width(); } const demo = document.getElementById("demo"); window.onresize = function() { demo.textContent = getScreenWidth(); console.clear(); console.log(demo.textContent); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="demo"></div>

Edit: You also cannot resize the browser window with JavaScript.编辑:您也不能使用 JavaScript 调整浏览器窗口的大小。 This would be a huge security issue, and you should be glad it's not possible.这将是一个巨大的安全问题,您应该庆幸这是不可能的。

From MDN :来自 MDN

The Screen interface represents a screen, usually the one on which the current window is being rendered, and is obtained using window.screen. Screen 接口代表一个屏幕,通常是当前窗口正在渲染的屏幕,它是使用 window.screen 获得的。

Thus, the screen object here is represents the device output display, and not the browser window.因此,这里的屏幕对象代表设备输出显示,而不是浏览器窗口。

Again, from MDN :再次, 来自 MDN

The Screen.width read-only property returns the width of the screen in pixels. Screen.width 只读属性返回以像素为单位的屏幕宽度。

Thus, screen.width (or window.screen.width ) does not give us the browser window width, but it's rather the screen (device output screen) width, which obvious would not change.因此, screen.width (或window.screen.width )没有给我们浏览器窗口宽度,而是屏幕(设备输出屏幕)宽度,这显然不会改变。

To get the width of the browser window, we need to use Element.clientWidth .要获取浏览器窗口的宽度,我们需要使用Element.clientWidth

From MDN once again:再次来自 MDN

The Element.clientWidth property is zero for inline elements and elements with no CSS;对于内联元素和没有 CSS 的元素,Element.clientWidth 属性为零; otherwise, it's the inner width of an element in pixels.否则,它是元素的内部宽度(以像素为单位)。 It includes padding but excludes borders, margins, and vertical scrollbars (if present).它包括填充但不包括边框、边距和垂直滚动条(如果存在)。

When clientWidth is used on the root element (the element), (or on if the document is in quirks mode), the viewport's width (excluding any scrollbar) is returned.当在根元素(元素)上使用 clientWidth 时(或者,如果文档处于怪癖模式),则返回视口的宽度(不包括任何滚动条)。

Just changing getScreenWidth() would do:只需更改getScreenWidth()

function getScreenWidth() {
    return document.body.clientWidth;
}

Now the console output changes when the width of the browser window changes.现在,当浏览器窗口的宽度发生变化时,控制台输出也会发生变化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM