Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got a couple of issues here.</p> <p>Firstly, you're defining your code within an <a href="http://www.ejball.com/EdAtWork/2005/03/28/JavaScriptAnonymousFunctions.aspx" rel="noreferrer">anonymous function</a>. This construct:</p> <pre><code>(function() { ... )(); </code></pre> <p>does two things. It defines an anonymous function and calls it. There are scope reasons to do this but I'm not sure it's what you actually want.</p> <p>You're passing in a code block to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.setTimeout" rel="noreferrer"><code>setTimeout()</code></a>. The problem is that <code>update()</code> is not within scope when executed like that. It however if you pass in a function pointer instead so this works:</p> <pre><code>(function() { $(document).ready(function() {update();}); function update() { $("#board").append("."); setTimeout(update, 1000); } } )(); </code></pre> <p>because the function pointer <code>update</code> is within scope of that block.</p> <p>But like I said, there is no need for the anonymous function so you can rewrite it like this:</p> <pre><code>$(document).ready(function() {update();}); function update() { $("#board").append("."); setTimeout(update, 1000); } } </code></pre> <p>or</p> <pre><code>$(document).ready(function() {update();}); function update() { $("#board").append("."); setTimeout('update()', 1000); } } </code></pre> <p>and both of these work. The second works because the <code>update()</code> within the code block is within scope now.</p> <p>I also prefer the <code>$(function() { ... }</code> shortened block form and rather than calling <code>setTimeout()</code> within <code>update()</code> you can just use <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval" rel="noreferrer"><code>setInterval()</code></a> instead:</p> <pre><code>$(function() { setInterval(update, 1000); }); function update() { $("#board").append("."); } </code></pre> <p>Hope that clears that up.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload