Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript DOM元素在setTimeout内被访问后神秘地消失_Javascript_Html - Fatal编程技术网

Javascript DOM元素在setTimeout内被访问后神秘地消失

Javascript DOM元素在setTimeout内被访问后神秘地消失,javascript,html,Javascript,Html,因此,我有一个函数pushSession,它可以有效地在给定的通信端口上建立串行连接。如果没有有效的连接,函数getCorrectDevice将拒绝其承诺并跳转到.catch 这里是函数本身- function pushSession(){ grabValidDevices() .then(checkDeviceSupport) .then(getCorrectDevice) .then(insertSessionHTML) .then(connectPo

因此,我有一个函数pushSession,它可以有效地在给定的通信端口上建立串行连接。如果没有有效的连接,函数getCorrectDevice将拒绝其承诺并跳转到.catch

这里是函数本身-

function pushSession(){
    grabValidDevices()
    .then(checkDeviceSupport)
    .then(getCorrectDevice)
    .then(insertSessionHTML)
    .then(connectPort)
    .catch(function(error){
        var flag = document.getElementById("error-flag")
        var errorContainer = document.getElementById("error-container")
        application.flags.push(error)
        flag.style.backgroundColor = "white"

        var count = 0
        //errorContainer.textContent = application.flags[count]
        count++

        setTimeout(function(errors){
            var flag = document.getElementById("error-flag")
            var errorContainer = document.getElementById("error-container")
            errorContainer.textContent = ""
            console.log("showing flag")
            flag.style.backgroundColor = "transparent"
            errors.flags = [] // once an error has been push to the UI, we can reset our flags
        }, 5000, application)    
    })
}
执行此代码时,在.catch结尾检索到的元素标志将消失-poof!-从我的DOM

在观察这种行为一段时间后,在我看来,改变元素样式的行为会导致元素从DOM中删除

在以下行调用函数pushSession后,元素立即消失,不再显示。这里真正奇怪的是,元素背景实际上变为白色,然后在setTimeout内5秒钟后再次透明。这会导致错误,告诉我变量标志未定义,因为DOM元素已不存在

我可以理解范围的问题,但这似乎与范围无关。正如我已经说过的,元素在DOM中不再存在

没有任何东西是从以前的传递过来的。然后打电话,所以我也相信这与我的承诺或他们的回报值无关

我突然想到,我的承诺将在执行setTimeout回调之前返回,但这不应该对回调的执行产生任何影响,而且我绝对看不到任何简单删除DOM元素的理由

对这种行为有什么解释吗


我在这里看到的唯一明显的解决方案是使用append-child动态添加元素,但这似乎有点不必要。

是的,正如我在评论中所说的,我有点猜测:因为error-flag是错误容器的子级,而您使用errorContainer.textContent=,清除错误容器,该标志将从DOM中消失:

错误标志是否可能是错误容器元素的子元素?是的!它是error-container中唯一的子元素。您设置了元素的文本内容,以便它将删除其子元素。。。你说flag是errorContainer的子元素,所以你删除了元素。我没有意识到这种行为。这就解释了问题所在。我想这意味着我将动态地重新插入flag元素。@Carl不要重新插入flag元素,你应该考虑一下你的DOM结构。我来试一试!