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

  • Home
  • SEARCH
  • 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 6781073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:34:53+00:00 2026-05-26T16:34:53+00:00

Let me start here. At w3cshools.com – http://www.w3schools.com/jsref/met_win_setinterval.asp – they have a snippet demo-ing

  • 0

Let me start here. At w3cshools.com – http://www.w3schools.com/jsref/met_win_setinterval.asp – they have a snippet demo-ing how to use javascript setInterval function (surprisingly it has a mismatched </form> but thats beside the point).

I needed to use setInterval() and at times I like referring to some “standard” body to take a glimpse of the recommended usage. In my development environment, something seems to be mangling setInterval() behavior/working when I use it within a jquery $(document).ready(function() { … }); block.

Illustration 1 – WORKS: Typical/Traditional <script> block

<script type="text/javascript">
    var refreshIntervalId;

    function testMessage() {
        window.alert("Hello");
    }
    refreshIntervalId = setInterval("testMessage()", 5000);
</script>

Illustration 2 – DOES NOT WORK: jQuery block

<script type="text/javascript">
    $(document).ready(function() {
        var refreshIntervalId;

        function testMessage() {
            window.alert("Hello");
        }
        refreshIntervalId = setInterval("testMessage()", 5000);
    });
</script>

Illustration 3 – WORKS:
jQuery block – using setInterval(testMessage, 5000) instead of setInterval(“testMessage()”, 5000)

<script type="text/javascript">
    $(document).ready(function() {
        var refreshIntervalId;

        function testMessage() {
            window.alert("Hello");
        }
        refreshIntervalId = setInterval(testMessage, 5000);
    });
</script>

It turns out that if I try to pass the function as a string from with the $(document).ready(function() {}); block, I get an error indicating that the function is not defined. Just so we dont get side-tracked IE, Chrome, and Firefox all report errors:

IE: Microsoft JScript runtime error: The value of the property ‘testMessage’ is null or undefined, not a Function object

Chrome: Uncaught ReferenceError: testMessage is not defined (anonymous function)

Firefox: testMessage is not defined.

What I would like to find out (if possible) is, could this be a result of standards non-conformance or what be going wrong when I try to use setInterval(“testMessage()”, 5000) from within the jQuery block? Could some mangling be happening or is this the right behavior?

  • 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-26T16:34:53+00:00Added an answer on May 26, 2026 at 4:34 pm

    setTimeout and setInterval break scope, so it can’t find testMessage when it goes looking for it (because that function is scoped inside the anonymous function you pass to ready). Browsers are behaving correctly.

    This is one of the reasons you should never, ever use the string format … or try to learn from the dreadful W3Schools.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all first post here :) Let's start with a snippet of the code
Ok here is my problem : Before i start the description, let me to
Let's start with the following snippet: Foreach(Record item in RecordList){ .. item = UpdateRecord(item,
Let me start of by saying that I am inexperienced with JavaScript. Here is
Let me start by acknowledging that I did find a similar question on here
Let me start by saying, I am new to Java programming. I have coded
Let me start off by apologizing for not giving a code snippet. The project
First let me start off by saying I read on here a lot and
Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/ ): object io {
Okay let me start off by saying I have been looking around a lot

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.