Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem accessing a function defined in Coffeescript
    primarykey
    data
    text
    <p>I'm converting some javascript to coffeescript, and I'm having trouble accessing a function I've defined. Here's the original working javascript (I'm also using jQuery):</p> <pre><code>function check_quiz_state(){ var div = $('#quiz-waiting'); var timestamp = new Date().getTime(); $.ajax({ url: window.location.pathname + "?time=" + timestamp, success: function(state) { if (state == 'created' || state == 'completed') { setTimeout("check_quiz_state()", 3000); } else if (state == 'built') { div.html("&lt;a href='" + window.location.pathname + "/pages/1'&gt;Click to begin!&lt;/a&gt;"); } else if (state == 'graded') { window.location.pathname = window.location.pathname + "/review" } } }); }; </code></pre> <p>After some cleanup and liberal use of the delete key, here's my coffeescript:</p> <pre><code>check_quiz_state = -&gt; success = (state) -&gt; switch state when 'created', 'completed' then setTimeout "check_quiz_state()", 3000 when 'built' then $('#quiz-waiting').html "&lt;a href='#{window.location.pathname}/pages/1'&gt;Click to begin!&lt;/a&gt;" when 'graded' then window.location.pathname = window.location.pathname + "/review" $.ajax {url: "#{window.location.pathname}?time=#{Date.now()}"}, success </code></pre> <p>The problem is with using setTimeout to make the function repeat - this works fine in the original javascript, but with the coffeescript it doesn't. I think it's unable to find the check_quiz_state function - if I use the javascript console in Chrome I can trigger the function just fine with my original javascript, but with the coffeescript version I get an error: "ReferenceError: check_quiz_state is not defined".</p> <p>What should I be doing differently?</p> <p>Edit - Here's what coffeescript is outputting. Sorry, slipped my mind:</p> <pre><code>(function() { var check_quiz_state; $(function() { // Other application code I omitted above, which is calling check_quiz_state() successfully. }); check_quiz_state = function() { var success; success = function(state) { switch (state) { case 'created': case 'completed': return setTimeout("check_quiz_state()", 3000); case 'built': return $('#quiz-waiting').html("&lt;a href='" + window.location.pathname + "/pages/1'&gt;Click to begin!&lt;/a&gt;"); case 'graded': return window.location.pathname = window.location.pathname + "/review"; } }; return $.ajax({ url: "" + window.location.pathname + "?time=" + (Date.now()) }, success); }; }).call(this); </code></pre> <p>I guess the function it's wrapped in is why I can't call it from the Chrome developer console, but I don't get why the timeout is failing. I'm not that great with javascript, though.</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.
 

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