Link to home
Create AccountLog in
JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

Avatar of bethKoala
bethKoala

my cursor won't change to an hourglass?
Using IE7:  I call a function that reads a text file, and I would like the cursor to change to an hourglass while the file is being processed.

document.body.style.cursor = "wait";
importTextFile();

function importTextFile()
{
      var fileString;
      var ctlStat = document.getElementById("status");
      ctlStat.innerHTML = "scanning file ...";
            
      var cFilename = document.getElementById("cFilename").value;
      cFilename = cFilename.replace(/^\s+|\s+$/g, '');

      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var file = fso.OpenTextFile(cFilename, 1, false);
      var nNumLines = 0;
      
      if (fso.FileExists(cFilename))
      {
            fileString = "";
            var strNextLine = "";
            while (!file.AtEndOfStream)
            {
                     strNextLine = file.ReadLine()
                  if (strNextLine.length > 0)
                  {
                        if (strNextLine.indexOf("&") >= 0)
                      {
                            strNextLine = strNextLine.replace(/&/g, '-');
                      }      
                      strNextLine = strNextLine + "\r\n";
                }      
                if (strNextLine.length > 0)
                {
                              fileString = fileString + strNextLine;
                              nNumLines ++
                              aFile.push(strNextLine);
                }
            }
            file.close();
      }
      ctlStat.innerHTML = "Done.";
      document.body.style.cursor = "default";
}

The cursor does not change, and also my status element is not updated with the assigned text ("scanning file"), while the file is being read.

I tried

     setTimeout("document.body.style.cursor = 'wait'", 10);

but it did not work.

Also tried

     setTimeout("importTextFile()",100);

but for some reason the file then was not processed?!

Thank you very much for any ideas.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Michel PlungjanMichel Plungjan🇩🇰

that is strange.

document.body.style.cursor = "wait";
setTimeout("importTextFile()",100);

SHOULD work with bot changing the cursor and calling the function
Are you sure you cleared your cache before trying the above?


read this for reasons not to change the cursor but instead use some image
http://ajaxpatterns.org/Progress_Indicator

Avatar of BigRatBigRat🇫🇷

The problem is that Javascript is executed on the same thread as the GUI, that is only when Javascript has finished executing does the screen get updated. Hence the cursor does not get repainted. The same happens with the clStat element. That won't get updated until it is finished.

Since FSO does not offer an asynchronous load, there is really no way around the problem, unless you write your own ActiveX control which does an asynchronous load.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


But if you change the cursor FIRST and then start the download, it should work

Avatar of BigRatBigRat🇫🇷

mplungian: How can you change the cursor AFTER starting the "download" when the FSO call is synchronous?

Did I say after? I did not. I said, changing the cursor BEFORE starting the download SHOULD work

If you change the cursor you change the cursor.
If you then 100 milliseconds AFTER changing the cursor you start the download, the cursor STAYS changed until something changes it back or unless the browser does not ALLOW you to change the cursor

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of BigRatBigRat🇫🇷

mplungian: I think you misunderstood my sarcasm. I apologize for making it in the first place. But if you read his code you'll see he does just what you suggest.

bethKoala: I have been doing a bit of research and here's a link which confirms my suspicions and suggests a solution :-

http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/257990100bd2383f

I did not see any sarcasm at all. Next time use a /sarcasm or don't try

I know he did, and it SHOULD work, so I wanted to make sure it was not due to something else.

Please show me the part in what you posted that is different to what he already tested and that I re-suggested and the solution?

So it works ONLY if the user moves the cursor!

here is a version that works in IE7 on XP

<html>
<head>
<script>
var theLink = null;
function tryOne(link) {
  document.body.style.cursor = "wait";
  theLink = link;
  theLink.style.cursor = "wait";
  setTimeout("importTextFile()",100);
}
 
function importTextFile() {
  for (var i=0;i<10000000;i++) ;
  document.body.style.cursor = "auto";
  if (theLink) theLink.style.cursor = "auto";
}
</script>
</head>
<body>
<a href="#" onClick="tryOne(this); return false">change and setTimout</a>
</body>
</html>

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of bethKoalabethKoala

ASKER

Thank you for your responses.  I am studying the code now.

I have looked at the links and your code and tried putting this together:

document.body.style.cursor = "wait";
setTimeout("checkCursor()",300);
importTextFile();
document.body.style.cursor = "default";

function checkCursor()
{
      if (document.body.style.cursor == "wait")
      {
            clearTimeout();
      }
      else
      {
            setTimeout("checkCursor()", 100);
      }
}

What happens now is that if I move the cursor off the window itself, it changes to a horizontal double-arrow.  Then at the very end it shows as an hourglass, briefly.

<< Are you sure you cleared your cache before trying the above? >>

Please tell me what the code would be for this?

Any further help is greatly appreciated.


1. no code to clear cache - internet options, temporary internet files

2. What is this supposed to do???

function checkCursor()
{
      if (document.body.style.cursor == "wait")
      {
            clearTimeout();
      }
      else
      {
            setTimeout("checkCursor()", 100);
      }
}

I see no reason for this script.

Did you try mine? Worked in IE7 as expected

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of BigRatBigRat🇫🇷

document.body.style.cursor = "wait";
setTimeout("doIimport()",300);

function doImport()
{
   importTextFile();
   document.body.style.cursor = "default";
}

erm yes. Like we have done now several times

Your code does work, thank you!  It allows the hourglass and status text to show.

But this is weird.  If I use the timeout on the first function that I call, then succeeding functions which depend on the results of the first, are not called.

setTimeout(importTextFile(),100);      this works and the hourglass shows
alert("got here 1");                             does not show
extractFromFile();                              not called ...
getProcs();                                        not called ...
doCompare();                      not called ...
alert("got here 2");                             does not show
saveReport();                                    not called

If I do not use the timeout, then everything is OK.  I have tried many things, such as bunching the functions into another and calling that one with a timeOut, calling a timeOut before each function, etc., all results are bad.  Do you know what I am doing wrong?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


If you do NOT use the quotes, the setTimeout will immediately execute the function and perform the RESULT of the function in 100 ms

You MUST use setTimeout('importTextFile()',100);
to execute the function in 100 ms

Thank you for spotting the quotes, I have fixed that.

My functions are now being executed in a different order?  I put in an alert at the end of importTextFile() and it shows after the saveReport()!  I don't think it is just a display timing issue, because the report is empty, as the lines from the text file are not being read until it is too late.  Without the setTImeout, it is OK.

Maybe my users will just have to trust me that things are in progress!  Why is this so hard?  Thanks for your patience.


Is it possible that the functions after importTextFile are being executed *before* importTextFile because of the delay in the setTimeout?  Is there a normal way to handle succeeding functions, where each one needs an hourglass to show that processing is happening?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Michel PlungjanMichel Plungjan🇩🇰

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Thank you for your help.
JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.