나는이 사실을 알지 못한다. getJson2 함수를 호출 한 후에 변수 "json"에 액세스 할 수 없습니다. PHP 스크립트를 통해 동적으로 json을 얻습니다. 그러나 그 후에 사라졌습니다. json이 init 함수에 포함되어있는 The InfoVis examples에서 지침으로 사용하는 샘플이 있습니다. 나는 그것을 거기에서 동적으로 얻으려고 노력하고있다.Javascript 변수 범위가 나를 방해하고있다
<script language="javascript" type="text/javascript">
var labelType, useGradients, nativeTextSupport,animate,json;
function getJson2()
{
var cd = getParameterByName("code");
$.get("tasks.php?code="+cd, function(data){
return data;
})
};
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
(function() {
var ua = navigator.userAgent,
iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
typeOfCanvas = typeof HTMLCanvasElement,
nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
textSupport = nativeCanvasSupport
&& (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
//I'm setting this based on the fact that ExCanvas provides text support for IE
//and that as of today iPhone/iPad current text support is lame
labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
nativeTextSupport = labelType == 'Native';
useGradients = nativeCanvasSupport;
animate = !(iStuff || !nativeCanvasSupport);
})();
debugger;
var Log = {
elem: false,
write: function(text){
if (!this.elem)
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
debugger;
this.elem.style.left = (500 - this.elem.offsetWidth/2) + 'px';
}
};
function init(){
json = getJson2();
//init data
var st = new $jit.ST({
//id of viz container element
injectInto: 'infovis',
//set duration for the animation
duration: 800,
//set animation transition type ..................
액세스 할 수 없습니까? 당신은 아무데도 접근하지 않고 있습니다. – phant0m
언제 getJson2를 호출합니까? – Christophe
'$ .get'은 비동기식입니다. [지연 객체] (http://api.jquery.com/category/deferred-object/)를 사용하지 않으면 콜백에서 돌아올 수 없습니다. – jbabey