While loops

Discussion in 'Ruby' started by Erich Lin, Jul 10, 2006.

  1. Erich Lin

    Erich Lin Guest

    If I have this snippet of code for the main page that loads:

    def index
    while 1
    logger.info("hi")
    end
    end

    Then, I close the browser that opened that page, I notice that the
    logger continues to print "hi" to the file (using tail -f). Is there a
    way to stop the server from running such actions when the browser is
    closed? (The logger.info was just an example. It could be anything
    else)

    Thanks
     
    Erich Lin, Jul 10, 2006
    #1
    1. Advertising

  2. "Erich Lin" <> writes:

    > If I have this snippet of code for the main page that loads:
    >
    > def index
    > while 1
    > logger.info("hi")
    > end
    > end
    >
    > Then, I close the browser that opened that page, I notice that the
    > logger continues to print "hi" to the file (using tail -f). Is there a
    > way to stop the server from running such actions when the browser is
    > closed? (The logger.info was just an example. It could be anything
    > else)
    >
    > Thanks


    When you close a page showing on the browser, the browser may or may
    not close the connection to your server. If it does not, then you
    can't tell whether a page has been closed.

    If it does, then you have to perform IO operation to detect the
    closing.

    If you really care, you could surround the critical code with guard
    code that tries to detect connection closing (IO::select for
    read-readyness and performing IO#sysread(nonzero length) on such IO
    object returns a 0-length string).

    But there is still no fool-proof way to detect a user closing a
    page. (Not even using javascript to handle window close event since
    that won't be invoked when the browser is closed).

    YS.
     
    Yohanes Santoso, Jul 10, 2006
    #2
    1. Advertising

  3. Erich Lin

    Laza Guest

    I assume you are talking about a Rails app. index() should NOT run
    forever like that. It should generate the page and return. If you want
    to keep updating someting, "pull" information from the browser using
    JavaScript (make the while loop in JavaScript).


    Erich Lin wrote:
    > If I have this snippet of code for the main page that loads:
    >
    > def index
    > while 1
    > logger.info("hi")
    > end
    > end
    >
    > Then, I close the browser that opened that page, I notice that the
    > logger continues to print "hi" to the file (using tail -f). Is there a
    > way to stop the server from running such actions when the browser is
    > closed? (The logger.info was just an example. It could be anything
    > else)
    >
    > Thanks
     
    Laza, Jul 11, 2006
    #3
    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. Angie

    while loops

    Angie, Jul 30, 2003, in forum: C++
    Replies:
    4
    Views:
    371
    Angie
    Aug 1, 2003
  2. for and while loops

    , Jun 28, 2006, in forum: Python
    Replies:
    6
    Views:
    390
    Bruno Desthuilliers
    Jun 29, 2006
  3. cman

    Testing pointers in while-loops

    cman, Mar 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    327
    Chris Thomasson
    Mar 3, 2007
  4. Zachary  Turner

    Variable declaration and while loops

    Zachary Turner, Jun 18, 2007, in forum: C++
    Replies:
    10
    Views:
    558
    Andre Kostur
    Jun 18, 2007
  5. Me
    Replies:
    2
    Views:
    247
Loading...

Share This Page