How to update a label using Javascript?

Discussion in 'Javascript' started by gnewsgroup, Dec 23, 2007.

  1. gnewsgroup

    gnewsgroup Guest

    In my asp.net 2.0 web application, I would like to display the process
    progress information in the browser. The progress info is displayed
    one after another with 1 or 2 seconds delay in between them. I
    believe you guys have seen this a lot in a DOS console.

    I have been trying this in my code-behind in C# for a few days with no
    luck.

    I think this must be achievable through javascript. But, I am new to
    javascript, could you guys give me a hint on how to do this in
    javascript? Thanks.
     
    gnewsgroup, Dec 23, 2007
    #1
    1. Advertising

  2. gnewsgroup said:
    > In my asp.net 2.0 web application, I would like to display the process
    > progress information in the browser. The progress info is displayed
    > one after another with 1 or 2 seconds delay in between them. I
    > believe you guys have seen this a lot in a DOS console.
    >
    > I have been trying this in my code-behind in C# for a few days with no
    > luck.
    >
    > I think this must be achievable through javascript. But, I am new to
    > javascript, could you guys give me a hint on how to do this in
    > javascript? Thanks.


    Where is the information displayed now? You could write the status
    message to a file on each poll and pick it up via a simple tag in the
    html, like:

    <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    then use javascript to display it every second with:

    <script type="text/javascript">
    function updateStatus() {
    document.getElementById("dataImg").src = "pageyouwroteto" +
    new Date().getTimeMillis();
    }
    window.setInterval(updateStatus, 1000);
    </script>

    Randy might have a better way, from some of the stuff I've seen him post.

    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #2
    1. Advertising

  3. My Pet Programmer said:
    > gnewsgroup said:
    >> In my asp.net 2.0 web application, I would like to display the process
    >> progress information in the browser. The progress info is displayed
    >> one after another with 1 or 2 seconds delay in between them. I
    >> believe you guys have seen this a lot in a DOS console.
    >>
    >> I have been trying this in my code-behind in C# for a few days with no
    >> luck.
    >>
    >> I think this must be achievable through javascript. But, I am new to
    >> javascript, could you guys give me a hint on how to do this in
    >> javascript? Thanks.

    >
    > Where is the information displayed now? You could write the status
    > message to a file on each poll and pick it up via a simple tag in the
    > html, like:
    >
    > <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    > then use javascript to display it every second with:
    >
    > <script type="text/javascript">
    > function updateStatus() {
    > document.getElementById("dataImg").src = "pageyouwroteto" +
    > new Date().getTimeMillis();
    > }
    > window.setInterval(updateStatus, 1000);
    > </script>
    >



    I forgot to put in how you display it after you get it, I'm sorry about
    that.

    Here you might have a label on the page, and you could display the
    status you got by modifying the function with:

    document.getElementById('labelid').innerHTML =
    document.getElementById("dataImg");

    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #3
  4. gnewsgroup

    David Mark Guest

    On Dec 23, 4:25 pm, My Pet Programmer <>
    wrote:
    > gnewsgroup said:
    >
    > > In my asp.net 2.0 web application, I would like to display the process
    > > progress information in the browser.  The progress info is displayed
    > > one after another with 1 or  2 seconds delay in between them.  I
    > > believe you guys have seen this a lot in a DOS console.

    >
    > > I have been trying this in my code-behind in C# for a few days with no
    > > luck.

    >
    > > I think this must be achievable through javascript.  But, I am new to
    > > javascript, could you guys give me a hint on how to do this in
    > > javascript?  Thanks.

    >
    > Where is the information displayed now? You could write the status
    > message to a file on each poll and pick it up via a simple tag in the
    > html, like:
    >
    > <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    > then use javascript to display it every second with:
    >
    > <script type="text/javascript">
    >    function updateStatus() {
    >      document.getElementById("dataImg").src = "pageyouwroteto" +
    >        new Date().getTimeMillis();


    Why getTimeMillis?

    >    }
    >    window.setInterval(updateStatus, 1000);


    As written, there is no way to stop this interval.

    > </script>
    >


    You are updating the src of a hidden image forever. What is the point
    of that?
     
    David Mark, Dec 23, 2007
    #4
  5. gnewsgroup

    David Mark Guest

    On Dec 23, 4:37 pm, My Pet Programmer <>
    wrote:
    > My Pet Programmer said:
    >
    >
    >
    >
    >
    > > gnewsgroup said:
    > >> In my asp.net 2.0 web application, I would like to display the process
    > >> progress information in the browser.  The progress info is displayed
    > >> one after another with 1 or  2 seconds delay in between them.  I
    > >> believe you guys have seen this a lot in a DOS console.

    >
    > >> I have been trying this in my code-behind in C# for a few days with no
    > >> luck.

    >
    > >> I think this must be achievable through javascript.  But, I am new to
    > >> javascript, could you guys give me a hint on how to do this in
    > >> javascript?  Thanks.

    >
    > > Where is the information displayed now? You could write the status
    > > message to a file on each poll and pick it up via a simple tag in the
    > > html, like:

    >
    > > <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">,and
    > > then use javascript to display it every second with:

    >
    > > <script type="text/javascript">
    > >   function updateStatus() {
    > >     document.getElementById("dataImg").src = "pageyouwroteto" +
    > >       new Date().getTimeMillis();
    > >   }
    > >   window.setInterval(updateStatus, 1000);
    > > </script>

    >
    > I forgot to put in how you display it after you get it, I'm sorry about
    > that.
    >
    > Here you might have a label on the page, and you could display the
    > status you got by modifying the function with:


    More likely a div than a label. But a label is what the OP seems to
    prefer.

    >
    > document.getElementById('labelid').innerHTML =
    > document.getElementById("dataImg");


    You are setting a string property to an element object. (?)

    It seems your solution needs a little more work (and explanation.)

    I am not sure what the OP wants, but this can't be it.
     
    David Mark, Dec 23, 2007
    #5
  6. David Mark said:
    > As written, there is no way to stop this interval.


    You're right, I didn't put a stop in. He should only have it run during
    the update and then stop. Excellent point, I should have paid better
    attention to detail.

    > Why getTimeMillis?


    Is there a performance hit, or just a curiosity. It makes it clearer
    what I'm doing in the code, I think. If it's inefficient, let me know
    and I'll just comment better.

    Ok, so an updated code listing then:

    <script type="text/javascript">

    function StatusUpdater(inID, outID) {
    this.inElm = document.getElementById(inID);
    this.outElm = document.getElementById(outID);
    this._interval;

    }

    StatusUpdater.prototype.start =
    function() {
    this._interval = window.setInterval(this.update, 1000);
    }

    StatusUpdater.prototype.stop =
    function() {
    window.clearInterval(this._interval);
    }

    StatusUpdater.prototype.update =
    function() {
    if (inElm && inElm.src) {
    if (outElm) {
    outElm.innerHTML = inElm.src;
    }
    }
    }

    And the usage would be:

    var updater = new StatusUpdater("myImageId", "myLabelId");
    updater.start();
    updater.stop();

    And you'd call updater.start() when you started the remote procedure,
    then updater.stop() when you finished.

    Is that a little beter?

    Thanks for the insight,
    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #6
  7. David Mark said:
    > On Dec 23, 4:37 pm, My Pet Programmer <>
    > wrote:
    >> My Pet Programmer said:
    >>
    >>
    >>
    >>
    >>
    >>> gnewsgroup said:
    >>>> In my asp.net 2.0 web application, I would like to display the process
    >>>> progress information in the browser. The progress info is displayed
    >>>> one after another with 1 or 2 seconds delay in between them. I
    >>>> believe you guys have seen this a lot in a DOS console.
    >>>> I have been trying this in my code-behind in C# for a few days with no
    >>>> luck.
    >>>> I think this must be achievable through javascript. But, I am new to
    >>>> javascript, could you guys give me a hint on how to do this in
    >>>> javascript? Thanks.
    >>> Where is the information displayed now? You could write the status
    >>> message to a file on each poll and pick it up via a simple tag in the
    >>> html, like:
    >>> <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    >>> then use javascript to display it every second with:
    >>> <script type="text/javascript">
    >>> function updateStatus() {
    >>> document.getElementById("dataImg").src = "pageyouwroteto" +
    >>> new Date().getTimeMillis();
    >>> }
    >>> window.setInterval(updateStatus, 1000);
    >>> </script>

    >> I forgot to put in how you display it after you get it, I'm sorry about
    >> that.
    >>
    >> Here you might have a label on the page, and you could display the
    >> status you got by modifying the function with:

    >
    > More likely a div than a label. But a label is what the OP seems to
    > prefer.
    >
    >> document.getElementById('labelid').innerHTML =
    >> document.getElementById("dataImg");

    >
    > You are setting a string property to an element object. (?)
    >
    > It seems your solution needs a little more work (and explanation.)
    >
    > I am not sure what the OP wants, but this can't be it.


    So be it, then. Not what he wants, that's fine. You can set the property
    to anything you like, though. It's definitely a hack, perhaps you could
    give him a better solution?

    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #7
  8. gnewsgroup

    gnewsgroup Guest

    On Dec 23, 4:57 pm, David Mark <> wrote:
    > On Dec 23, 4:37 pm, My Pet Programmer <>
    > wrote:
    >
    >
    >
    > > My Pet Programmer said:

    >
    > > > gnewsgroup said:
    > > >> In my asp.net 2.0 web application, I would like to display the process
    > > >> progress information in the browser. The progress info is displayed
    > > >> one after another with 1 or 2 seconds delay in between them. I
    > > >> believe you guys have seen this a lot in a DOS console.

    >
    > > >> I have been trying this in my code-behind in C# for a few days with no
    > > >> luck.

    >
    > > >> I think this must be achievable through javascript. But, I am new to
    > > >> javascript, could you guys give me a hint on how to do this in
    > > >> javascript? Thanks.

    >
    > > > Where is the information displayed now? You could write the status
    > > > message to a file on each poll and pick it up via a simple tag in the
    > > > html, like:

    >
    > > > <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    > > > then use javascript to display it every second with:

    >
    > > > <script type="text/javascript">
    > > > function updateStatus() {
    > > > document.getElementById("dataImg").src = "pageyouwroteto" +
    > > > new Date().getTimeMillis();
    > > > }
    > > > window.setInterval(updateStatus, 1000);
    > > > </script>

    >
    > > I forgot to put in how you display it after you get it, I'm sorry about
    > > that.

    >
    > > Here you might have a label on the page, and you could display the
    > > status you got by modifying the function with:

    >
    > More likely a div than a label. But a label is what the OP seems to
    > prefer.
    >
    >
    >
    > > document.getElementById('labelid').innerHTML =
    > > document.getElementById("dataImg");

    >
    > You are setting a string property to an element object. (?)
    >
    > It seems your solution needs a little more work (and explanation.)
    >
    > I am not sure what the OP wants, but this can't be it.


    OK, what I want is something like this (in C# instead of JavaScript,
    to get my idea understood. BTW, we see such stuff all the time in
    Thread programming tutorials.)

    myLabel.Text = "Something to display...<br/>";
    Thread.Sleep(1000);
    myLabel.Text += "Something else to display...<br/>";
    Thread.Sleep(1000);
    myLabel.Text += "Some other stuff to show ... <br/>";

    This will not work in an asp.net page. The page will be presented to
    the browser after all of the output has been completed, and thus users
    will see all 3 outputs above all at once.

    It'll work if I do Response.Write in-line in the markup with
    Response.Buffer = false. But, then it is hard to put these output in
    a well-formatted page.

    I hope that I have made myself understood.
     
    gnewsgroup, Dec 23, 2007
    #8
  9. gnewsgroup

    David Mark Guest

    On Dec 23, 5:08 pm, My Pet Programmer <>
    wrote:
    > David Mark said:
    >  > As written, there is no way to stop this interval.
    >
    > You're right, I didn't put a stop in. He should only have it run during
    > the update and then stop. Excellent point, I should have paid better
    > attention to detail.
    >
    >  > Why getTimeMillis?
    >
    > Is there a performance hit, or just a curiosity. It makes it clearer
    > what I'm doing in the code, I think. If it's inefficient, let me know
    > and I'll just comment better.


    What's wrong with getTime? I've never even heard of getTimeMillis.

    >
    > Ok, so an updated code listing then:
    >
    > <script type="text/javascript">
    >
    > function StatusUpdater(inID, outID) {
    >    this.inElm = document.getElementById(inID);
    >    this.outElm = document.getElementById(outID);
    >    this._interval;
    >


    That line doesn't do anything.

    > }
    >
    > StatusUpdater.prototype.start =
    >    function() {
    >      this._interval = window.setInterval(this.update, 1000);
    >    }
    >
    > StatusUpdater.prototype.stop =
    >    function() {
    >     window.clearInterval(this._interval);
    >    }
    >
    > StatusUpdater.prototype.update =
    >    function() {
    >      if (inElm && inElm.src) {
    >        if (outElm) {
    >          outElm.innerHTML = inElm.src;
    >        }
    >      }
    >    }


    You are confused. Did you try running this? Even if you straighten
    out the errors, the best it will do is set the innerHTML of an element
    to a URI.

    >
    > And the usage would be:
    >
    > var updater = new StatusUpdater("myImageId", "myLabelId");
    > updater.start();
    > updater.stop();
    >
    > And you'd call updater.start() when you started the remote procedure,
    > then updater.stop() when you finished.
    >
    > Is that a little beter?


    Not really.
     
    David Mark, Dec 23, 2007
    #9
  10. gnewsgroup

    David Mark Guest

    On Dec 23, 5:10 pm, My Pet Programmer <>
    wrote:
    > David Mark said:
    >
    >
    >
    >
    >
    > > On Dec 23, 4:37 pm, My Pet Programmer <>
    > > wrote:
    > >> My Pet Programmer said:

    >
    > >>> gnewsgroup said:
    > >>>> In my asp.net 2.0 web application, I would like to display the process
    > >>>> progress information in the browser.  The progress info is displayed
    > >>>> one after another with 1 or  2 seconds delay in between them.  I
    > >>>> believe you guys have seen this a lot in a DOS console.
    > >>>> I have been trying this in my code-behind in C# for a few days with no
    > >>>> luck.
    > >>>> I think this must be achievable through javascript.  But, I am new to
    > >>>> javascript, could you guys give me a hint on how to do this in
    > >>>> javascript?  Thanks.
    > >>> Where is the information displayed now? You could write the status
    > >>> message to a file on each poll and pick it up via a simple tag in the
    > >>> html, like:
    > >>> <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    > >>> then use javascript to display it every second with:
    > >>> <script type="text/javascript">
    > >>>   function updateStatus() {
    > >>>     document.getElementById("dataImg").src = "pageyouwroteto" +
    > >>>       new Date().getTimeMillis();
    > >>>   }
    > >>>   window.setInterval(updateStatus, 1000);
    > >>> </script>
    > >> I forgot to put in how you display it after you get it, I'm sorry about
    > >> that.

    >
    > >> Here you might have a label on the page, and you could display the
    > >> status you got by modifying the function with:

    >
    > > More likely a div than a label.  But a label is what the OP seems to
    > > prefer.

    >
    > >> document.getElementById('labelid').innerHTML =
    > >> document.getElementById("dataImg");

    >
    > > You are setting a string property to an element object. (?)

    >
    > > It seems your solution needs a little more work (and explanation.)

    >
    > > I am not sure what the OP wants, but this can't be it.

    >
    > So be it, then. Not what he wants, that's fine. You can set the property
    > to anything you like, though. It's definitely a hack, perhaps you could
    > give him a better solution?


    I don't understand your attempted solutions, nor do I fully comprehend
    what the OP wants.
     
    David Mark, Dec 23, 2007
    #10
  11. gnewsgroup

    David Mark Guest

    On Dec 23, 5:36 pm, gnewsgroup <> wrote:
    > On Dec 23, 4:57 pm, David Mark <> wrote:
    >
    >
    >
    >
    >
    > > On Dec 23, 4:37 pm, My Pet Programmer <>
    > > wrote:

    >
    > > > My Pet Programmer said:

    >
    > > > > gnewsgroup said:
    > > > >> In my asp.net 2.0 web application, I would like to display the process
    > > > >> progress information in the browser.  The progress info is displayed
    > > > >> one after another with 1 or  2 seconds delay in between them.  I
    > > > >> believe you guys have seen this a lot in a DOS console.

    >
    > > > >> I have been trying this in my code-behind in C# for a few days withno
    > > > >> luck.

    >
    > > > >> I think this must be achievable through javascript.  But, I am new to
    > > > >> javascript, could you guys give me a hint on how to do this in
    > > > >> javascript?  Thanks.

    >
    > > > > Where is the information displayed now? You could write the status
    > > > > message to a file on each poll and pick it up via a simple tag in the
    > > > > html, like:

    >
    > > > > <img id="dataImg" src="pageyouwroteto" style="visibility:hidden">, and
    > > > > then use javascript to display it every second with:

    >
    > > > > <script type="text/javascript">
    > > > >   function updateStatus() {
    > > > >     document.getElementById("dataImg").src = "pageyouwroteto" +
    > > > >       new Date().getTimeMillis();
    > > > >   }
    > > > >   window.setInterval(updateStatus, 1000);
    > > > > </script>

    >
    > > > I forgot to put in how you display it after you get it, I'm sorry about
    > > > that.

    >
    > > > Here you might have a label on the page, and you could display the
    > > > status you got by modifying the function with:

    >
    > > More likely a div than a label.  But a label is what the OP seems to
    > > prefer.

    >
    > > > document.getElementById('labelid').innerHTML =
    > > > document.getElementById("dataImg");

    >
    > > You are setting a string property to an element object. (?)

    >
    > > It seems your solution needs a little more work (and explanation.)

    >
    > > I am not sure what the OP wants, but this can't be it.

    >
    > OK, what I want is something like this (in C# instead of JavaScript,
    > to get my idea understood. BTW, we see such stuff all the time in
    > Thread programming tutorials.)
    >
    > myLabel.Text = "Something to display...<br/>";
    > Thread.Sleep(1000);
    > myLabel.Text += "Something  else to display...<br/>";
    > Thread.Sleep(1000);
    > myLabel.Text += "Some other stuff to show ... <br/>";
    >
    > This will not work in an asp.net page.  The page will be presented to
    > the browser after all of the output has been completed, and thus users
    > will see all 3 outputs above all at once.
    >
    > It'll work if I do Response.Write in-line in the markup with
    > Response.Buffer = false.  But, then it is hard to put these output in
    > a well-formatted page.
    >
    > I hope that I have made myself understood.


    Write an element, then write script blocks that update the innerHTML
    (or innerText) property of that element. If you need help with the
    JavaScript involved, see the FAQ.
     
    David Mark, Dec 23, 2007
    #11
  12. Randy Webb said:
    >
    > I'm not sure how to take that.
    >


    It was meant respectfully. I'm going to lay back and watch for a while,
    since it seems I'm not paying any attention to detail.

    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #12
  13. David Mark said:
    > On Dec 23, 5:08 pm, My Pet Programmer <>
    > wrote:
    >> David Mark said:
    >> > As written, there is no way to stop this interval.

    >>
    >> You're right, I didn't put a stop in. He should only have it run during
    >> the update and then stop. Excellent point, I should have paid better
    >> attention to detail.
    >>
    >> > Why getTimeMillis?

    >>
    >> Is there a performance hit, or just a curiosity. It makes it clearer
    >> what I'm doing in the code, I think. If it's inefficient, let me know
    >> and I'll just comment better.

    >
    > What's wrong with getTime? I've never even heard of getTimeMillis.
    >
    >> Ok, so an updated code listing then:
    >>
    >> <script type="text/javascript">
    >>
    >> function StatusUpdater(inID, outID) {
    >> this.inElm = document.getElementById(inID);
    >> this.outElm = document.getElementById(outID);
    >> this._interval;
    >>

    >
    > That line doesn't do anything.
    >
    >> }
    >>
    >> StatusUpdater.prototype.start =
    >> function() {
    >> this._interval = window.setInterval(this.update, 1000);
    >> }
    >>
    >> StatusUpdater.prototype.stop =
    >> function() {
    >> window.clearInterval(this._interval);
    >> }
    >>
    >> StatusUpdater.prototype.update =
    >> function() {
    >> if (inElm && inElm.src) {
    >> if (outElm) {
    >> outElm.innerHTML = inElm.src;
    >> }
    >> }
    >> }

    >
    > You are confused. Did you try running this? Even if you straighten
    > out the errors, the best it will do is set the innerHTML of an element
    > to a URI.
    >
    >> And the usage would be:
    >>
    >> var updater = new StatusUpdater("myImageId", "myLabelId");
    >> updater.start();
    >> updater.stop();
    >>
    >> And you'd call updater.start() when you started the remote procedure,
    >> then updater.stop() when you finished.
    >>
    >> Is that a little beter?

    >
    > Not really.


    Ok, you got me, I shouldn't have replied to this at all. I don't know
    what the hell I'm even thinking today, but I sure as hell shouldn't be
    writing code I don't know enough about.

    ~A!
     
    My Pet Programmer, Dec 23, 2007
    #13
  14. Randy Webb said:

    > And if you don't write it, you won't learn it, and where will that get
    > you? I have had my head bashed in so many times here over code I posted
    > I stopped letting it bother me.
    >

    Sounds like a plausible way to look at things, thanks for that. I'm
    reading that best practices doc over at Javascript Toolbox right now,
    actually.

    ~A!
     
    My Pet Programmer, Dec 24, 2007
    #14
  15. In comp.lang.javascript message <fkmjnj$q27$>
    , Sun, 23 Dec 2007 16:25:02, My Pet Programmer
    <> posted:

    > new Date().getTimeMillis();


    Which browsers implement that?

    Please post only code that has been tested, and use copy'n'paste to do
    it.

    --
    (c) John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.
     
    Dr J R Stockton, Dec 24, 2007
    #15
  16. gnewsgroup

    ~A! Guest

    On Dec 24, 1:06 pm, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <fkmjnj$>
    > , Sun, 23 Dec 2007 16:25:02, My Pet Programmer
    > <> posted:
    >
    > > new Date().getTimeMillis();

    >
    > Which browsers implement that?
    >
    > Please post only code that has been tested, and use copy'n'paste to do
    > it.
    >

    None, actually. It was an egregious and disgusting public display of
    incompetence and lack of attention to detail. I've learned my lesson,
    and I will not let it happen again.

    ~A!
     
    ~A!, Dec 25, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    760
    Scott Mitchell [MVP]
    May 7, 2005
  2. Josef K.

    <label><div></div></label> allowed?

    Josef K., May 20, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    13,674
    Josef K.
    May 22, 2005
  3. Praveen
    Replies:
    1
    Views:
    1,032
    Rob Williscroft
    Nov 5, 2006
  4. CSharpner
    Replies:
    0
    Views:
    1,109
    CSharpner
    Apr 9, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    309
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page