简体   繁体   English

span的文本不会立即被Javascript更改

[英]text of span doesn't get changed with Javascript immediately

I have a span and a button. 我有一个跨度和一个按钮。 When I press the button I want to change the span and call a function (getting a private key with keythereum , but I don't think it matters what function I use), which takes a pretty long time to finish (usually 5-10 seconds). 当我按下按钮时,我想更改跨度并调用一个函数(使用keythereum获取私钥,但我认为使用哪个函数并不重要),这需要很长时间才能完成(通常为5-10)秒)。

window.myFunction = function() {

// checking the text of the span
    console.log(document.getElementById("span").textContent);

//changing my span
    document.getElementById("span").textContent = "Please wait..."

// console outputs the new span showing that the textContent has changed
    console.log(document.getElementById("span").textContent);

// getting the password from an input field
    var password = document.getElementById("password").value;

// calling the function to get a private Key from an object, 
// locked with the password I just got from the input field
    try {
        var privateKey = keythereum.recover(password, keyObj);
        console.log(privateKey.toString('hex')); 
    } catch (error) {
        console.log(error);
    }
}

When I press the button I want the span to change to something like "please wait..." and then to call the function. 当我按下按钮时,我希望跨度更改为“ please wait ...”,然后调用该函数。 But what happens when I press the button is that I get the first console log with the old textContent of the span, next I get the new console log with the new textContent but I cannot see the change on the screen. 但是,当我按下按钮时会发生什么,我得到的第一个控制台日志带有跨度的旧textContent,接下来我得到的新控制台日志带有新的textContent,但是我看不到屏幕上的更改。 Next nothing happens while the keythereum function gets called. 接下来,在调用keythereum函数时没有任何反应。 The span only visually changes when the function completes it's task and the privateKey gets logged. 仅当函数完成任务并记录privateKey时,范围才在视觉上改变。 Is there a way to immediately visually change the span before the function gets called? 有没有一种方法可以在调用函数之前立即以可视方式更改跨度?

keythereum.recover apparently does synchronous blocking (eg, uses alert or prompt or confirm ). keythereum.recover显然不同步阻塞(例如,使用alertpromptconfirm )。 This prevents the change from being painted. 这样可以防止更改被绘制。

To give the browser a chance to paint the change, schedule the code after the text change for the next task in the task queue via setTimeout : 为了使浏览器有机会绘制更改,请在文本更改后通过setTimeout为任务队列中的下一个任务安排代码:

window.myFunction = function() {

// checking the text of the span
    console.log(document.getElementById("wallet-warning").textContent);

//changing my span
    document.getElementById("span").textContent = "Please wait..."

//***do the rest after the browser has a chance to repain
    setTimeout(function()) {
// console outputs the new span showing that the textContent has changed
        console.log(document.getElementById("wallet-warning").textContent);

// getting the password from an input field
        var password = document.getElementById("password").value;

// calling the function to get a private Key from an object, 
// locked with the password I just got from the input field
        try {
            var privateKey = keythereum.recover(password, keyObj);
            console.log(privateKey.toString('hex')); 
        } catch (error) {
            console.log(error);
        }
    }, 50); // 50ms
}

50ms is virtually unnoticeable to humans, but is a long time to the browser. 50ms实际上对于人类来说并不明显,但是对于浏览器来说却是很长的时间。 (I used to use 0 , which schedules it to run as soon as possible once the current task [and its microtasks] complete, but Firefox doesn't always repaint in that case.) (我曾经使用0 ,它调度它在当前任务[及其微任务]完成后尽快运行,但是在这种情况下Firefox并不总是重新绘制。)

Make the call to keythereum.recover() asynchronous using something like setTimeout(). 使用setTimeout()之类的方法异步调用keythereum.recover() ()。 The text of the span doesn't update because the browser isn't updating the DOM until your JavaScript finishes. 跨度的文本不会更新,因为在JavaScript完成之前浏览器不会更新DOM。

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

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