Solved

force refresh/repaint screen while processing?

Posted on 2003-11-21
5
1,433 Views
Last Modified: 2008-03-17
I have something like:

for(i=0; i<10; i++)
{
  document.all.textboxProcess.value = i;
  process(i);
}

But the value of textboxProcess is not updated until the loop finish.
How can I force screen refresh/repaint inside the loop?
0
Comment
Question by:Hardi
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
splamunge earned 30 total points
ID: 9801996
You have to give the GDI a timeslice, so it can repaint, try ...

<html>
  <body>
    <form>
      Test value here: <input type=text id="textboxProcess" name="textboxProcess" size=8 maxlength="8" value="-1">
    </form>
    <script language="javascript">
      var upperLimit = 10, updateTO = 0, msec = 100;
     
      function advanceTick(newTick){
        if(!newTick){newTick = 0;}
        if(newTick < upperLimit){
          document.all.textboxProcess.value = newTick.toString();
          newTick++;
          updateTO = window.setTimeout('advanceTick(' + newTick + ')', msec);
        }
      }
     
      onload=advanceTick;
    </script>
  </body>
</html>

... Adjust msec upward or downwards until it looks right.
0
 
LVL 6

Author Comment

by:Hardi
ID: 9802051
umm... thanks
so there is no way you can do such as
window.repaint;
or anything?

i'll try that advanceTick...
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9802053
Don't use document.all. You scripts will run much more efficiently without it, and your scripts will also run cross-browser.

Use document.forms[0].textboxPorcess.value or document.getElementById("textboxProcess").value instead:

<html>
<head>
<script language="javascript">
  var upperLimit = 10, msec = 200;

  function advanceTick()
  {
    var newTick = document.getElementById("textboxProcess").value;
    if(newTick < upperLimit)
    {
      document.forms[0].textboxProcess.value = ++newTick;
      timer = setTimeout("advanceTick()", msec);
    }
  }

  window.onload = advanceTick;
</script>
</head>
<body>
<form>
Test value here: <input type="text" id="textboxProcess" name="textboxProcess" size="8" maxlength="8" value="-1">
</form>
</body>
</html>

Also the toString() call isn't necessary, and you can just get the value from the text input.

This also has the advantage that it will work in all JS-capable browsers.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9802078
> window.repaint;

Clientside JS doesn't give you that sort of fine-grained control over graphics/screen.

Remember, a Web browser is essentially just a glorified document viewer. ;^)
0
 
LVL 6

Author Comment

by:Hardi
ID: 9802381
thanks splamunge and Zontar
my code is now messed up coz it's not that simple =P
but i know how to use setTimeout
and i'll start using getelementbyid =)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question