简体   繁体   English

使用setinterval时如何停止属性的初始跳转

[英]how to stop initial jump of the attributes while using setinterval

i am using set interval for displaying a dynamic data in progress but here my issue is on the initial start of the page it is kind of jumping the elements and then displaying the data .how can i stop that jump and here on initial my progress bar is also showing from middle not from the start 我正在使用设置的间隔来显示正在进行的动态数据,但是在这里我的问题是在页面的初始位置,它有点是在跳过元素然后显示数据。我如何才能停止这种跳转,在这里我的进度条也从中间开始显示

 var timerId = 0; $(document).ready(function() { loadData(); }); function loadData() { var data = { information: { "num": 73.00, "value": 75 } } var i = 0; var maxValue = data["information"]["value"]; timerId = setInterval(function() { data.information.value += 1; data.information.num += 0.01; let tri = data.information.num.toFixed(2); $('.animation').text(tri); $('.process').text(data.information.value); $('#blips > .progress-bar').css("width", data.information.value + "%"); i++; if (data.information.value == 100) { clearInterval(timerId); } }, 1000) } $('#stop').click(function() { clearInterval(timerId); }); 
 .text { position: absolute; z-index: 999; margin: 0 auto; left: 0; right: 0; text-align: center; top: 19%; font-family: Arial, sans-serif; color: black; width: 62%; } .nimation {} .progress-bar { background-color: brown !important; width: 244px; height: 44px; } #stop { position: relative; margin-left: 112px; left: 0px; margin-top: 46px; width: 288px; height: 80px; border-radius: 5px; background: brown; color: white; } .process { font-size: 36px; margin-left: 26%; } .para { font-size: 36px; color: black; margin-top: -5rem; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div> <div class="text"> <p class="animation"> </p> <p class="para">Data</p> </div> <br> <br> <div class="progress" id="blips"> <div class="progress-bar" role="progressbar"> <span class="sr-only"></span> </div> </div> <p class="process"></p> <br> <button class="btn btn-primary" id="stop">Stop</button> </div> </div> 

Put the code that updates the DOM from the data object in a separate function that you call before starting the interval timer, so that you'll display the data with the initial values first. 在启动间隔计时器之前,将用于更新data对象中DOM的代码放在单独的函数中,以便您首先显示带有初始值的数据。

 var timerId = 0; $(document).ready(function() { loadData(); }); function loadData() { var data = { information: { "num": 73.00, "value": 75 } } var i = 0; var maxValue = data["information"]["value"]; function displayProgress() { let tri = data.information.num.toFixed(2); $('.animation').text(tri); $('.process').text(data.information.value); $('#blips > .progress-bar').css("width", data.information.value + "%"); } displayProgress(); timerId = setInterval(function() { data.information.value += 1; data.information.num += 0.01; displayProgress(); i++; if (data.information.value == 100) { clearInterval(timerId); } }, 1000) } $('#stop').click(function() { clearInterval(timerId); }); 
 .text { position: absolute; z-index: 999; margin: 0 auto; left: 0; right: 0; text-align: center; top: 19%; font-family: Arial, sans-serif; color: black; width: 62%; } .nimation {} .progress-bar { background-color: brown !important; width: 244px; height: 44px; } #stop { position: relative; margin-left: 112px; left: 0px; margin-top: 46px; width: 288px; height: 80px; border-radius: 5px; background: brown; color: white; } .process { font-size: 36px; margin-left: 26%; } .para { font-size: 36px; color: black; margin-top: -5rem; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div> <div class="text"> <p class="animation"> </p> <p class="para">Data</p> </div> <br> <br> <div class="progress" id="blips"> <div class="progress-bar" role="progressbar"> <span class="sr-only"></span> </div> </div> <p class="process"></p> <br> <button class="btn btn-primary" id="stop">Stop</button> </div> </div> 

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

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