控制台错误说全局变量未定义

时间:2012-12-18 05:22:00

标签: javascript json chap-links-library

我正在使用此插件http://almende.github.com/chap-links-library/js/timeline/doc/从一些JSON数据中呈现时间轴。这是完整的相关代码:

<script>
function doEverything() {
var timeline;

$.getJSON("tljson.json",function(result){
  items = [JSON.stringify(result)];
  drawVisualization();
});

function drawVisualization() {

var options = {'width':  '100%'};

timeline = new links.Timeline(document.getElementById('mytimeline'));
        timeline.draw(window.items, options);
 };
 }
</script>
<body onload="doEverything();">

当我提醒'window.items'时;它按预期显示。但是,检查器上的控制台显示

Uncaught TypeError: Cannot call method 'valueOf' of undefined 

时间轴不呈现。我做错了什么?

编辑:

console.log(JSON.stringify(result))的输出:

[{"content":"rubygem-aws-2.6.0 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=863368'>View on BugZilla</a>","start":"863368"},{"content":"Recent update of rubygem-nokogiri breaks rubygem-aws-sdk<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=816706'>View on BugZilla</a>","start":"816706"},{"content":"Please update rubygem-aws-sdk to 1.3.5<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=800616'>View on BugZilla</a>","start":"800616"},{"content":"Review Request: rubygem-aws-sdk - AWS SDK for Ruby<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=723472'>View on BugZilla</a>","start":"723472"},{"content":"Please add EPEL branches for rubygem-aws<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=723152'>View on BugZilla</a>","start":"723152"},{"content":"Unable to resolve dependency of rubygem-aws-2.4.2.2-2.fc15 (updates)<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=716343'>View on BugZilla</a>","start":"716343"},{"content":"rubygem-aws-2.5.7 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=703433'>View on BugZilla</a>","start":"703433"},{"content":"rubygem-aws-2.5.1 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=703117'>View on BugZilla</a>","start":"703117"},{"content":"rubygem-aws-2.4.5 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=691728'>View on BugZilla</a>","start":"691728"},{"content":"Update rubygem-aws to 2.3.34<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=668955'>View on BugZilla</a>","start":"668955"},{"content":"Review Request: rubygem-aws - Ruby gem for all Amazon Web Services<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=637378'>View on BugZilla</a>","start":"637378"}]

编辑:最新代码:(控制台仍然返回timeline.draw的未知数据类型):

<script>
function doEverything() {
var timeline;

$.getJSON("tljson.json",function(result){
drawVisualization(result);
});

function drawVisualization(result) {
//console.log (JSON.stringify(result));
var options = {
            'width':  '100%'
        };

timeline = new links.Timeline(document.getElementById('mytimeline'));
    timeline.draw(JSON.stringify(result), options);
};
}
</script>
<body onload="doEverything();">

2 个答案:

答案 0 :(得分:1)

您应该将项目作为参数传递给drawVisualization。请不要计划将变量添加到全局命名空间。这是更正后的代码:

<script>
  function doEverything() {
    var timeline;

    $.getJSON("tljson.json",function(result){
      drawVisualization(result);
    });

    function drawVisualization(items) {

        var options = {'width':  '100%'};
        console.log(items);
        var timeline = new links.Timeline(document.getElementById('mytimeline'));
        timeline.draw(items, options);
     }
   }
</script>
<body onload="doEverything();">

答案 1 :(得分:0)

似乎您的变量items无法识别,因为您尚未将其声明为全局变量。你可以先这样声明:

<script>
var items;

function doEverything() {
    var timeline;

    // rest of your code
}