简体   繁体   English

除非打开Web检查器,否则Safari上的HTML5 Progress元素不会更改值

[英]HTML5 Progress Element on Safari won't change value unless the Web Inspector is open

I've been using a progress element to display a battery value which is assigned over Javascript whenever a button is clicked. 我一直在使用progress元素来显示电池值,每当单击按钮时,电池值就会通过Javascript分配。 This value updates reliably on every browser besides Safari. 除了Safari之外,此值在所有浏览器上都能可靠更新。

For some reason this progress element will only update it's value while the Web Inspector is open, until that point it will wrongly display a bar about 10% full. 由于某种原因,此进度元素仅在Web Inspector打开时才会更新其值,直到该点它会错误地显示大约10%的填充条。

Below is the switch case used to assign the value: 以下是用于分配值的开关盒:

    switch(x) { 
    case (x = "critical"):
        document.getElementById('batBar').value = "10";
        break;
    case (x = "low"):
        document.getElementById('batBar').value = "25";
        break;
    case (x = "half"):
        document.getElementById('batBar').value = "50";
        break;
    case (x = "high"):
        document.getElementById('batBar').value = "75";
        break;
    case (x = "full"):
        document.getElementById('batBar').value = "100";
        break;
    case (x = "charging"):
        document.getElementById('batBar').value = "100";
        break;
    case (x = "charged"):
        document.getElementById('batBar').value = "100";
        break;
    }

The element itself: 元素本身:

<div class="settingsline">
    <label>Battery 
        <progress id="batBar" max="100" value="60"></progress> 
    </label>
</div>

I was wondering if there was a reason that this might be happening as researching the issue is turning up nothing. 我想知道是否有可能发生这种情况,因为研究该问题没有发现任何问题。

UPDATE: I spotted and fixed the problem by changing the syntax of the switch-case; 更新:我通过更改开关盒的语法发现并解决了该问题; I'm not sure how I didn't notice this. 我不确定我怎么没注意到这一点。 My question now is how it even worked in the first place and why opening the inspector fixed this. 现在我的问题是,它首先是如何工作的,为什么打开检查器可以解决此问题。

Your switch syntax is incorrect. 您的switch语法不正确。 You are using assignment declarations in each case statement, which is changing the x variable and causing unexpected behavior. 您在每个case语句中使用赋值声明,这会更改x变量并导致意外行为。 Your switch-statement should look more like this: 您的切换语句应如下所示:

switch(x) { 
    case "critical":
       ...
       break;
    case "low":
       ...
       break;
}

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

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