【问题标题】:coffee-script try and catch failing in IE8咖啡脚本尝试在 IE8 中捕获失败
【发布时间】:2012-06-01 01:25:55
【问题描述】:
Radiant::loadCSS = (fn, scope) ->
  head = document.getElementsByTagName("head")[0]
  link = document.createElement("link")
  link.setAttribute "href", "/" + ri.context + "/css/" + @obj + ".css"
  link.setAttribute "rel", "stylesheet"
  link.setAttribute "type", "text/css"
  sheet = undefined
  cssRules = undefined
  if "sheet" of link
    sheet = "sheet"
    cssRules = "cssRules"
  else
    sheet = "styleSheet"
    cssRules = "rules"
  timeout_id = setInterval(->
    try
      if link[sheet] and link[sheet][cssRules].length
        clearInterval timeout_id
        clearTimeout timeout_id
        fn.call scope or window, true, link #LINE THAT ERRORS OUT!!!
    #finally
  , 10)
  timeout_id = setTimeout(->
    clearInterval timeout_id
    clearTimeout timeout_id
    head.removeChild link
    fn.call scope or window, false, link
  , 15000)
  head.appendChild link
  link

所以上面是我从这里撕下来的函数的翻译:Dynamically loading css file using javascript with callback without jQuery

当我在直接的 javascript 中使用它时效果很好,但是咖啡版本在 ie8 'fn' is null or not an object 中给我带来了问题

这是该部分的渲染 javascript:

timeout_id = setInterval(function() {
  try {
    if (link[sheet] && link[sheet][cssRules].length) {
      clearInterval(timeout_id);
      clearTimeout(timeout_id);
      return fn.call(scope || window, true, link); //ERROR LINE!!!
    }
  } catch (_error) {}
}, 10);

无论如何,我是 trycatch 的新手,只是不明白这里出了什么问题。提前谢谢大家!

【问题讨论】:

    标签: javascript internet-explorer-8 coffeescript try-catch


    【解决方案1】:

    我认为这与 try/catch 的关系较小,而与此有关,无论出于何种原因,fn 在回调执行时未定义(如错误所示)。在出现错误的情况下,您确定 fn 被传递到 loadCSS 吗?

    【讨论】:

    • 哦,呵呵。这次我没有传入回调函数。
    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2016-08-20
    相关资源
    最近更新 更多