www.webdeveloper.com
Results 1 to 7 of 7

Thread: how to track Variable without using old-school alert('variable'); ..?

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    how to track Variable without using old-school alert('variable'); ..?

    i'm returning, or trying to get back into JS programming.

    what's a better way to track a variable, better than using alert('variable')?

    thanks,

    trying to catch up here.

  2. #2
    Join Date
    Dec 2012
    Posts
    54
    I depends on the situation and the web browser it appears. If the browser being used allows Javascript to run in the main GUI thread then the window won't update until the javascript is finished processing meaning the variable won't be displayed until the end.

    https://bugzilla.mozilla.org/show_bug.cgi?id=62778

    if you use an interval

    http://nokarma.org/2011/02/02/javasc...oop/index.html

    then you could update the inner html of a "debugging" div.

    Hope that makes sense.

  3. #3
    Join Date
    Dec 2012
    Posts
    54
    Here is a small example::

    Code:
    <!DOCTYPE html>
    <html lang = "en-US">
    <head>
    </head>
    <body onload = "DoIt()">
      <div id = "debugger" style = "left: 5%; top: 5%; height: 100px; width: 100px; position: absolute; background: #EEEEFF;">
      </div>
      <script>
        var x = 0;
        var IntervalId;
        function countup()
        {
          x++;
          var Debug = document.getElementById("debugger");
          Debug.innerHTML = x;
        }
        function DoIt()
        {
          IntervalID = setInterval(countup, 1);
        }
      </script>
    </body>
    </html>

  4. #4
    Join Date
    Mar 2011
    Posts
    1,159
    If an alert() won't help me, I generally use a variation on the method shown above by Lesshardtoofind, you can also use Firebug in Firefox, the JavaScript Console in Chrome, or Internet Explorer's built-in debugging tools to monitor variables and set breakpoints.

  5. #5
    Join Date
    Dec 2012
    Posts
    54
    I use the Firefox tools as well. Highly informative!

  6. #6
    Join Date
    Mar 2010
    Posts
    281
    huge thanks everyone!

  7. #7
    Join Date
    Dec 2012
    Posts
    21
    Don't forget about console.log(). It will give you a detailed breakdown of the contents of any variable; its particularly useful for objects (ie arrays)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles