Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6214439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:52:36+00:00 2026-05-24T06:52:36+00:00

I am trying to build a timer. Please compare the two situations (the first

  • 0

I am trying to build a timer. Please compare the two situations (the first one works, not the second):

  • inline javascript http://jsfiddle.net/x7xhA/
  • non-inline javascript http://jsfiddle.net/x7xhA/1/

What is the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:52:37+00:00Added an answer on May 24, 2026 at 6:52 am

    This is a commonly encountered problem with users of jsFiddle’s ‘JavaScript section’. You see, the code that’s put into the ‘JavaScript section’ is wrapped within a function used as a load handler, so in your second example, the real output result is this:

      <script type='text/javascript'>
      //<![CDATA[ 
      $(window).load(function(){
      var seconds = 0;
    
    function timedCount() {
        $("#txt").val(seconds);
        seconds += 1;
        setTimeout("timedCount()",1000);
    }
      });
      //]]> 
      </script>
    

    Now, timedCount isn’t a global function anymore, as it’s available in the scope of the load handler only, and when you use setTimeout with a string of code, this gets evaluated from the global scope.

    Ways to fix this include:

    change the setTimeout call to setTimeout(timedCount, 1000);

    What this does, is passes the actual function object to setTimeout. Rather than evaluate the string of code, from global scope, each time, this essentially preserves the ability to call the function as scope doesn’t matter anymore – you’re handing the function to setTimeout.

      var seconds = 0;
    
    function timedCount() {
        $("#txt").val(seconds);
        seconds += 1;
        setTimeout(timedCount,1000);
    }
    

    make timedCount a global function using timedCount = function() { ... };

    This merely makes timedCount a global, so that when setTimeout tries to evaluate timedCount(); from the global scope, it succeeds as there is a timedCount function in the global scope.

      var seconds = 0;
    
    timedCount = function() {
        $("#txt").val(seconds);
        seconds += 1;
        setTimeout("timedCount();",1000);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
I'm trying build a regex that will replace any characters not of the format:
I'm trying to build binutils 2.21.1a from http://ftp.gnu.org/gnu/binutils on Solaris sun4u sparc SunOS 5.10.
Today I've installed Bada SDK and trying first time to build some apps on
I've been trying to improve the build times for my product. We're using continuous
I'm currently trying to build an application that inherently needs good time synchronization across
I've been trying to build subversion (on a limited account) for a long time
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
Trying to build sslsniff on a RHEL 5.2 system here. When compiling sslsniff on
I trying to build and compile my xcodeproj in command line and it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.