访问 Coffeescript 中定义的函数时出现问题
我正在将一些 javascript 转换为 Coffeescript,但在访问我定义的函数时遇到问题。这是原始的工作 javascript(我也使用 jQuery):
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("<a href='" + window.location.pathname + "/pages/1'>Click to begin!</a>");
}
else if (state == 'graded') {
window.location.pathname = window.location.pathname + "/review"
}
}
});
};
经过一些清理和自由使用删除键后,这是我的咖啡脚本:
check_quiz_state = ->
success = (state) ->
switch state
when 'created', 'completed' then setTimeout "check_quiz_state()", 3000
when 'built' then $('#quiz-waiting').html "<a href='#{window.location.pathname}/pages/1'>Click to begin!</a>"
when 'graded' then window.location.pathname = window.location.pathname + "/review"
$.ajax {url: "#{window.location.pathname}?time=#{Date.now()}"}, success
问题在于使用 setTimeout 使函数重复 - 这在原始 javascript 中工作正常,但是而咖啡脚本则不然。我认为它无法找到 check_quiz_state 函数 - 如果我在 Chrome 中使用 javascript 控制台,我可以使用原始 javascript 触发该函数,但是使用 Coffeescript 版本时,我收到错误:“ReferenceError:check_quiz_state 未定义”。
我应该采取什么不同的做法?
编辑 - 这是 Coffeescript 输出的内容。抱歉,我忘记了:
(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("<a href='" + window.location.pathname + "/pages/1'>Click to begin!</a>");
case 'graded':
return window.location.pathname = window.location.pathname + "/review";
}
};
return $.ajax({
url: "" + window.location.pathname + "?time=" + (Date.now())
}, success);
};
}).call(this);
我猜它包含的函数是我无法从 Chrome 开发者控制台调用它的原因,但我不明白为什么超时失败。不过,我对 javascript 不太擅长。
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):
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("<a href='" + window.location.pathname + "/pages/1'>Click to begin!</a>");
}
else if (state == 'graded') {
window.location.pathname = window.location.pathname + "/review"
}
}
});
};
After some cleanup and liberal use of the delete key, here's my coffeescript:
check_quiz_state = ->
success = (state) ->
switch state
when 'created', 'completed' then setTimeout "check_quiz_state()", 3000
when 'built' then $('#quiz-waiting').html "<a href='#{window.location.pathname}/pages/1'>Click to begin!</a>"
when 'graded' then window.location.pathname = window.location.pathname + "/review"
$.ajax {url: "#{window.location.pathname}?time=#{Date.now()}"}, success
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".
What should I be doing differently?
Edit - Here's what coffeescript is outputting. Sorry, slipped my mind:
(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("<a href='" + window.location.pathname + "/pages/1'>Click to begin!</a>");
case 'graded':
return window.location.pathname = window.location.pathname + "/review";
}
};
return $.ajax({
url: "" + window.location.pathname + "?time=" + (Date.now())
}, success);
};
}).call(this);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
噢。愚蠢的错误。当我从 javascript 翻译为 Coffeescript 时,我搞砸了 ajax 调用。
应该是:
对不起大家。
D'oh. Stupid mistake. I screwed up the ajax call when I was translating from javascript to coffeescript.
Should be:
Sorry, everyone.