【问题标题】:Getting "(anonymous function)" error in the terminal在终端中出现“(匿名函数)”错误
【发布时间】:2023-03-16 13:21:01
【问题描述】:

我正在尝试使用 link 在我的 rails 应用程序中实现速度计

但我收到以下错误

Uncaught ReferenceError: google is not defined
(anonymous function)

这是我的示例咖啡脚本代码

$(document).ready ->
  drawChart = ->
      data = google.visualization.arrayToDataTable([
        [
          'Label'
          'Value'
        ]
        [
          'Memory'
          80
        ]
        [
          'CPU'
          55
        ]
        [
          'Network'
          68
        ]
      ])
      options = 
        width: 400
        height: 120
        redFrom: 90
        redTo: 100
        yellowFrom: 75
        yellowTo: 90
        minorTicks: 5
      chart = new (google.visualization.Gauge)(document.getElementById('widget-inner'))
      chart.draw data, options
      setInterval (->
        data.setValue 0, 1, 40 + Math.round(60 * Math.random())
        chart.draw data, options
        return
      ), 13000
      setInterval (->
        data.setValue 1, 1, 40 + Math.round(60 * Math.random())
        chart.draw data, options
        return
      ), 5000
      setInterval (->
        data.setValue 2, 1, 60 + Math.round(20 * Math.random())
        chart.draw data, options
        return
      ), 26000
      return
    
    google.load 'visualization', '1.0', 'packages': [ 'gauge' ]
    google.setOnLoadCallback drawChart
    

这是我的 index.html.haml 文件,我试图在其中添加给定的速度计。

.content-container.widgets-container
  %h2 "This is Client Manager Dashboad page"
  = link_to "Add New Widget", admin_widgets_path, :class => "btn btn-info"
  .widgets-wrapper
    #widget-1.col-sm-4
      .widget-inner#widget-inner

我还在布局中添加了 jsapi 脚本文件

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

谁能告诉我如何解决给错误。

我正在使用 rails 3.2.11ruby 1.9.3

【问题讨论】:

  • 在您开始使用 google 函数之前,似乎没有加载 Jsapi。从 sn-ps 很难判断为什么会发生这种情况。有没有办法让你在使用 google 函数之前确保 jsapi 已经加载?

标签: jquery ruby-on-rails ruby jsapi


【解决方案1】:

$(document).ready 启动时,jsapi 脚本似乎还没有完成加载。就绪并不意味着全部加载。将 jsapi 脚本包含在 $(document).ready 脚本之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多