简体   繁体   English

HTML/CSS/Javascript:强制重绘 javascript function 中的元素,由 onclick 调用

[英]HTML/CSS/Javascript: Force redraw of element in javascript function called by onclick

Fairly new to front end development and in need of some help.对前端开发相当陌生,需要一些帮助。

I have a simple button that uses onclick to call a javascript function.我有一个简单的按钮,它使用 onclick 来调用 javascript function。 This function queries a server for some data, and can often take a minute or two.这个 function 向服务器查询一些数据,通常需要一两分钟。 I'd like to display a loading bar to indicate progress while the data is being prepared.我想在准备数据时显示一个加载栏来指示进度。 The problem is that the HTML/CSS I'm inserting and changing via javascript is not rendering until after the entire javascript function has returned, which is no good if I need it to change while the function is doing things. The problem is that the HTML/CSS I'm inserting and changing via javascript is not rendering until after the entire javascript function has returned, which is no good if I need it to change while the function is doing things. Is there a way I can force a redraw of the element within the javascript function after I have inserted the html or changed the styling?在插入 html 或更改样式后,有没有办法强制重绘 javascript function 中的元素? Another question I have is if this is an inherent problem with using onclick.我的另一个问题是,这是否是使用 onclick 的固有问题。 I have called javascript functions before by inserting a call in a element and this seems to work.我之前通过在元素中插入调用来调用 javascript 函数,这似乎有效。

So far I have this a CSS file with the following progress bar styling information:到目前为止,我有一个 CSS 文件,其中包含以下进度条样式信息:

#progress-back {
  width: 100%;
  height: 30px;
  background-color: grey;
}

#progress-bar {
  width: 1%;
  height: 30px;
  background-color: green;
}

a button and div in my html:我的 html 中的按钮和 div:

<button type="button" onclick="loadData(form)">Load</button>
<div id="progress"></div>

then my javascript function:然后我的 javascript function:

function loadData(form){
    // Insert the progress bar elements.
    document.getElementById('progress').innerHTML = `
        <div id="progress-back">
            <div id="progress-bar"></div>
        </div>
    `
    for(var i = 0; i < nQueries; i++){
        // code that gets data and concats.
        // ...

        // move bar based on percent complete.
        moveBar(((i+1)/nQueries)*100)
    }
}

function moveBar(percent){
    document.getElementById("progress-bar").style.width = percent+"%"
}

I know the javascript function is being called because I'm getting the data, and by the end of it, the progress bar is there, 100% filled.我知道 javascript function 被调用是因为我正在获取数据,到最后,进度条就在那里,100% 填充。

Cheers干杯

Either your CSS is incorrect or your moveBar() function is not getting executed properly.您的 CSS 不正确或您的 moveBar() function 未正确执行。

In your moveBar() comment out the code and put console.log( percent ) and see if it prints out the correct value.在您的moveBar()中注释掉代码并放入console.log( percent )并查看它是否打印出正确的值。

If it is printing out the correct value then your CSS is wrong.如果它打印出正确的值,那么您的 CSS 是错误的。

Reply to this answer what happens when you do the steps I listed above.回复此答案当您执行我上面列出的步骤时会发生什么。

The problem was that I was using XMLHttpRequest and not running asynchronously.问题是我使用的是 XMLHttpRequest 而不是异步运行。 Cheers干杯

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

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