• (cs)

    This post misses the code where it actually triggers this method... Normally AJAX calls don't trigger scripts to run again unless this method is called in the oncomplete callback.

    The code is still ugly tho..

  • Nobulate (unregistered)

    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!

  • YellowOnline (unregistered)

    I don't know AJAX, but using .ToString when working with time must be wrong in pretty much every language.

    Captcha: appellatio - a sexual act performed by Mac-zealots

  • rohcQaH (unregistered)

    How to spot someone who doesn't understand javascript, without even looking:

    theTime = window.setTimeout("runLogoutClock()", 1000);

    The callback right there? That's an eval() in disguise. It's ugly and discouraged, it doesn't play nice with strict mode, it prevents the JS interpreter from optimizing or JITing, it prevents strong minification.. and it's entirely pointless. Just pass the function directly:

    theTime = window.setTimeout(runLogoutClock, 1000);

    Anytime you spot some javascript that uses strings as callbacks, you know the coder hasn't refreshed his knowledge since the 90s, and probably shouldn't be near a keyboard. I think the example in the article proves that point.

  • Stefan (unregistered) in reply to rohcQaH

    At least he included the base argument with every parseInt call.

  • Egbert (unregistered) in reply to rohcQaH
    rohcQaH:
    since the 90s

    It could have been written in the nineties.

  • Meep (unregistered)
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.

    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.

  • (cs) in reply to Egbert
    Egbert:
    rohcQaH:
    since the 90s

    It could have been written in the nineties.

    I'd like access to the time machine that allows people to write ASP.NET (released 2002) in the 90s.

  • rohcQaH (unregistered) in reply to Egbert
    Egbert:
    It could have been written in the nineties.
    Unlikely. For example, getElementById() and cross-browser innerHTML are post-2000. And this
    var modal = $find('ctl00_mpeLogout');
    modal.show();
    is surely part of some relatively modern javascript framework. Those just didn't exist in the 90s, and judging by the code they wouldn't write one themselves. Using $ as a prefix for framework-functions is a recent thing, too.

    Modal dialogs in the 90s were called window.alert().

  • faoileag (unregistered) in reply to RaceProUK
    RaceProUK:
    Egbert:
    rohcQaH:
    since the 90s
    It could have been written in the nineties.
    I'd like access to the time machine that allows people to write ASP.NET (released 2002) in the 90s.
    As long as they are sitting in their time machine while hacking away at .NET code on their notebooks, this should not present much of a problem (bar building the time machine, of course).

    But it is beside the point - this is not .NET code, this is Javascript.

  • faoileag (unregistered) in reply to Meep
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
  • faoileag (unregistered)

    I still fail to see a major wtf, though - runLogoutClock() is called once when the webpage is loaded (being in the global namespace) and then only everytime window.setTimeout() calls it. It is not automagically called on AJAX calls.

    The string stuff is ugly but a minor wtf.

    Accurracy might not be great, but for an automated log off, who cares if instead of after 10 minutes log off happens after 10:42 or premature at 9:58?

    What's missing is the reset to 10 minutes once the user does something, but that can happen elsewhere since the counter is not a javascript variable but the (string)inner html of an element.

    So yeah, a bit ugly, but who cares. A quick-and-dirty automatic log off.

  • MiniMax (unregistered)

    It is not surprising that programmers have problems with time. Even Nobel prize winners don't know what time is, except "that thing which prevents everything from happening instantaneous" (which would be bad in so many ways).

    Captha: Jumentum - things that are happening just this moment.

  • Anon (unregistered) in reply to YellowOnline
    YellowOnline:
    I don't know AJAX, but using .ToString when working with time must be wrong in pretty much every language.

    Clearly you don't know AJAX, because it isn't a language.

  • (cs) in reply to faoileag
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    Does it happen to throw your CPU load through the roof? Sleep is still the most efficient way I can think of to wait for a given time. Check the clock and update the countdown control.
  • (cs) in reply to Nobulate
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!

    But but... Just yesterday I got yelled at for saving a phone number as an integer instead of as a string? Oh, this programming stuff is hard to do!

  • Duncan Simpson (unregistered) in reply to RichP

    Using an integer for a telephone number is genuinely a bad idea because, at least where I live, telephone numbers start with 0 (domestic) or 00 (international). Integers reliably don't record leading 0s.

    On the original problem anyone vaguely sane would check that the session was not timed out when somebody requested some information or pressed a form submit button. The whole approach is WTF.

    If you want hard programming try writing a parallel program framework where you have to concentrate on both getting the details right and preserving the properties your deadlock freedom proof requires simultaneously.

  • C-Derb (unregistered) in reply to rohcQaH
    rohcQaH:
    How to spot someone who doesn't understand javascript, without even looking:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing? Feel something without touching? Hear something without listening? Taste something without eating?
  • Anon (unregistered) in reply to RichP
    RichP:
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!

    But but... Just yesterday I got yelled at for saving a phone number as an integer instead of as a string? Oh, this programming stuff is hard to do!

    Can you do math with time?

    Can you do math with phone numbers?

    That's the difference.

  • Nitpicker (unregistered) in reply to C-Derb
    C-Derb:
    rohcQaH:
    How to spot someone who doesn't understand javascript, without even looking:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing? Feel something without touching? Hear something without listening? Taste something without eating?

    I can feel alone and taste beer, at the very least.

  • Anon (unregistered) in reply to C-Derb
    C-Derb:
    rohcQaH:
    How to spot someone who doesn't understand javascript, without even looking:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing?

    Yes, you can smell something without breathing. Take something smelly, hold your breathe and then waft it under your nose. You will still smell it as the aroma chemicals move into your nose. You smell in your nose, not your lungs.

    This is a common trick for chemists to smell something without taking a lung full of (potentially harmful) chemicals.

  • (cs) in reply to RaceProUK
    RaceProUK:
    Egbert:
    rohcQaH:
    since the 90s
    It could have been written in the nineties.
    I'd like access to the time machine that allows people to write ASP.NET (released 2002) in the 90s.
    Barack Obama has it - he used it to go back in time and place that phoney birth announcement in the Honolulu papers. He got it from the Warren Commission, who used it to go back in time and frame Oswald for the CIA's killing of JFK. I think the Warren Commission got it from FDR...
  • (cs) in reply to faoileag
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    X = Current_Time; While Current_Time - X < 10minutes; Print 10minutes + X - Current_Time; End;
  • (cs) in reply to Anon
    Anon:
    RichP:
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!

    But but... Just yesterday I got yelled at for saving a phone number as an integer instead of as a string? Oh, this programming stuff is hard to do!

    Can you do math with time?

    Can you do math with phone numbers?

    That's the difference.

    There are a LOT of programmers who can't do math with time!

  • (cs) in reply to Some Damn Yank
    Some Damn Yank:
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    X = Current_Time; While Current_Time - X < 10minutes; Print 10minutes + X - Current_Time; End;
    CPU hog. Stick a "sleep 60" in there and you won't be hysterically tight-looping. Not to mention the miles of virtual paper used up with all those Print statements.
  • (cs) in reply to faoileag
    faoileag:
    But it is beside the point - this is not .NET code, this is Javascript.
    This code isn't, but I think he was referring to the "ctl00_" prefixed IDs, which suggests that this JavaScript is on an ASP.NET page.
  • (cs)

    It can be done without sleep. That is point previous poster is trying to prove.

  • nmclean (unregistered) in reply to Meep
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.

    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.

    If we want to figure out when processing a file is done, then yes we should wait for an "I'm done processing this file" signal instead of polling it with sleeps in between.

    But if the signal is "a second has passed", as it would be when implementing a countdown timer, then sleep is exactly what we want. Then we check the actual time so that if 1.1 actual seconds have passed then we know to ask for a "0.9 seconds have passed" signal next time.

  • Shill (unregistered) in reply to Nagesh
    Nagesh:
    It can be done without sleep. That is point previous poster is trying to prove.

    Except that it can't unless that is the ONLY thing you want the browser to do.

  • TheDoctor (unregistered)

    Frist (but appearing last due to temporal distortion)

  • chernobyl (unregistered)

    If the setTimeout method were a real, synchronous sleep command, it would be easier and maybe it would work (it'd still be awful code). But in fact that's not what I'd call sleep, it is just setting an event handler (like pretty much everything you do in Javascript). Lots of things can happen in the meantime (i.e. other code doing stuff) so you shouldn't rely on it running neatly in 1s intervals and everything staying the same in between.

  • Not Somebody (unregistered) in reply to faoileag
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.

    Sure, it's called alarm() and signal()

    captcha: pecus - pecaus I can read man pages

  • Dominic (unregistered)

    snoofle for president!

  • (cs) in reply to C-Derb
    C-Derb:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing? Feel something without touching? Hear something without listening? Taste something without eating?

    Feel something without touching: A nice fire Taste something without eating: "That leaves a bad taste in my mouth" Hear something without listening: Wife?

    So there you go!

  • feugiat (unregistered) in reply to Not Somebody
    Not Somebody:
    faoileag:
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.

    Sure, it's called alarm() and signal()

    captcha: pecus - pecaus I can read man pages

    But apparently you can't read requirements.

  • (cs)

    TRWTF is thinking that the data should be held in a control instead of simply displayed there. That was the developer's first mistake.

  • (cs) in reply to chubertdev
    chubertdev:
    TRWTF is thinking that the data should be held in a control instead of simply displayed there. That was the developer's first mistake.

    I'm sorry: this is written in ASP.NET, using default controls with the default naming convention, as far as we can see it imports an entire js framework to get access to one function ($find), it performs string manipulation to decrement a value, and it uses client side code to attempt to implement a "security" feature - but your biggest issue with it is storing data in a control?!

    tbh, I wouldn't be at all surprised to find that getComputedStyle(document.getElementById('ctl00_lblTimer')) had a display:none; declaration in there somewhere: the existence of "ctl00_lblWaitClock" and "runWaitForClock()" send shivers down my spine.

    Although as already noted, setTimeout() is a very poor substitute for a properly implemented sleep, and I'd be frankly amazed if this code actually worked in any meaningful way...

  • (cs) in reply to JimM
    JimM:
    chubertdev:
    TRWTF is thinking that the data should be held in a control instead of simply displayed there. That was the developer's first mistake.

    I'm sorry: this is written in ASP.NET, using default controls with the default naming convention, as far as we can see it imports an entire js framework to get access to one function ($find), it performs string manipulation to decrement a value, and it uses client side code to attempt to implement a "security" feature - but your biggest issue with it is storing data in a control?!

    tbh, I wouldn't be at all surprised to find that getComputedStyle(document.getElementById('ctl00_lblTimer')) had a display:none; declaration in there somewhere: the existence of "ctl00_lblWaitClock" and "runWaitForClock()" send shivers down my spine.

    Although as already noted, setTimeout() is a very poor substitute for a properly implemented sleep, and I'd be frankly amazed if this code actually worked in any meaningful way...

    I'm sorry, you think that default names will open the gates to hell?

  • Bill C. (unregistered) in reply to C-Derb
    C-Derb:
    rohcQaH:
    How to spot someone who doesn't understand javascript, without even looking:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing? Feel something without touching? Hear something without listening? Taste something without eating?
    Sleep without grinding? In blissful slumber? Yielding for milliseconds? Hey, who invited Hillary?
  • Norman Diamond (unregistered) in reply to Some Damn Yank
    Some Damn Yank:
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    X = Current_Time; While Current_Time - X < 10minutes; Print 10minutes + X - Current_Time; End;
    In a country with daylight savings time, everyone will get logged out instantaneously.
  • Norman Diamond (unregistered) in reply to Some Damn Yank
    Some Damn Yank:
    RaceProUK:
    Egbert:
    rohcQaH:
    since the 90s
    It could have been written in the nineties.
    I'd like access to the time machine that allows people to write ASP.NET (released 2002) in the 90s.
    Barack Obama has it - he used it to go back in time and place that phoney birth announcement in the Honolulu papers. He got it from the Warren Commission, who used it to go back in time and frame Oswald for the CIA's killing of JFK. I think the Warren Commission got it from FDR...
    The US Department of Justice has one too. They took documents that were created in 2003 and 2004, and destroyed the documents in 1990. The court expected me to use a time machine too.
  • Little Lib (unregistered) in reply to Nobulate
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!
    yesterday same
  • Mitch (unregistered) in reply to Nitpicker
    Nitpicker:
    C-Derb:
    rohcQaH:
    How to spot someone who doesn't understand javascript, without even looking:
    I know it is possible to smoke something without inhaling, but can you also smell something without breathing? Feel something without touching? Hear something without listening? Taste something without eating?

    I can feel alone and taste beer, at the very least.

    And you can hear things without listening - eg when someone talks to you and you hear that they are talking but at the end of theirtalk you have no idea what they said.

  • I tor (unregistered)

    A) Much better article snoofle B) That'sthe first I've seen Nagesh for a while, and his Engrish has improved markedly

  • QJo (unregistered) in reply to RichP
    RichP:
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!

    But but... Just yesterday I got yelled at for saving a phone number as an integer instead of as a string? Oh, this programming stuff is hard to do!

    Programming is hard! Let's go shopping!

  • Franky (unregistered)

    timers are not easy ... some jquery stopwatch plugin for instance was too stupid to get it right (they slept for 960ms which caused the clock to run faster until 1000 and 960 sync up again which would cause the clock to stall)

  • faoileag (unregistered) in reply to Little Lib
    Nobulate:
    Amazingly no one thought to store the time in an integer, as seconds. Variable rate of decrement I can live with (think The Doppler Effect), but string manipulation? Guffah and Inexcusable!
    Unfortunately, if you did that, you would have to transform the epoch time into something legible, e.g. 9:59.

    What is faster (decrementing a string "9:59" or transforming 599 into "9:59") I don't know but you would probably not gain much.

  • (cs) in reply to chubertdev
    chubertdev:
    I'm sorry, you think that default names will open the gates to hell?

    Well, I have personal experience of watching client side code break horribly between a test server deployment and a live server deployment due to ASP.NET default naming and unnamed containing controls. Both servers had identical configurations, as far as anyone could tell. Trying to resolve it felt like I'd been at least dragged into Purgatory, if not Hell itself...

    Any time your client side code relies on server-side automagical naming, you're asking for trouble. How hard is it to name the containing control yourself?

    Although frankly, anyone who uses Webforms as is deserves throwing into hell anyway. IMNSHO, of course.

  • anonymous (unregistered) in reply to faoileag
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    The setTimeout function is a proper signal, not a sleep. I don't know WTF Meep was complaining about; Javascript doesn't actually have any way to sleep.

    There are no real WTFs in today's code (well, except for the bizarre white-bold-italics-on-red that was used for the single-quoted strings).

    Using setTimeout("runLogoutClock()", 1000) instead of setTimeout(runLogoutClock, 1000) falls under "good enough". It works, the overhead is negligible (you're only executing it once per second, for fuck's sake; that is an eternity to the computer).

    Yes, it's true that the clock won't run at exactly one second per tick, but there's absolutely no reason to be worried about it drifting a few milliseconds. It's a logout timer; high precision is not necessary.

    The only possible reason this could be a WTF would depend on the code that calls runLogoutClock, because the way it's written, it can't be called more than once or your clock will be decremented more than once per second.

  • Anon (unregistered) in reply to anonymous
    anonymous:
    faoileag:
    Meep:
    Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.
    No, they figure out how to properly signal when things are done, and don't use sleep as a crutch.
    So please, post your sleep-less solution to the given problem ("display a ten-minute countdown on a webpage using Javascript; the visible remaining time should be accurate to one second") here in the comments section of TDWTF for our pleasure.
    The setTimeout function is a proper signal, not a sleep. I don't know WTF Meep was complaining about; Javascript doesn't actually have any way to sleep.

    There are no real WTFs in today's code (well, except for the bizarre white-bold-italics-on-red that was used for the single-quoted strings).

    Using setTimeout("runLogoutClock()", 1000) instead of setTimeout(runLogoutClock, 1000) falls under "good enough". It works, the overhead is negligible (you're only executing it once per second, for fuck's sake; that is an eternity to the computer).

    Yes, it's true that the clock won't run at exactly one second per tick, but there's absolutely no reason to be worried about it drifting a few milliseconds. It's a logout timer; high precision is not necessary.

    The only possible reason this could be a WTF would depend on the code that calls runLogoutClock, because the way it's written, it can't be called more than once or your clock will be decremented more than once per second.

    More to the point, regardless of what language you are using, if you want millisecond resolution, you need a real-time operating system, which is something most people aren't running.

Leave a comment on “The Temporal Countdown Timer”

Log In or post as a guest

Replying to comment #:

« Return to Article