两个外部文件之间的Javascript变量

时间:2016-07-05 19:33:52

标签: javascript function variables external

我是JS的新手,我不明白为什么这不起作用。以下是名为test1.js

的文件
var greeting;

function randomGreeting(){
    var greet = new Array("BORING JS TEST ALERT", "EXCITING JS TEST ALERT", "AWESOME JS TEST ALERT");
    var randGreet = Math.floor(Math.random() * greet.length);

    greeting = {
        alert: greet[randGreet]
    };
}

在单独的test2.js文件中:

alert(greeting.alert);

在我的HTML中,我在正文randomGreeting()中调用了onLoad,我在test1.js之前加载了test2.js,但我仍然从undefined获取 String access_token = object.get("access_token").toString(); System.out.println("Access Token -------->" + access_token); System.out.println("Decoded Access Token --------> " +URLDecoder.decode(access_token)); //String basicAuth = "Basic "+ new String(new Base64().encode(access_token.getBytes())); CreateSubmissionMetadata(access_token); public static void CreateSubmissionMetadata (String access_token) throws ClientProtocolException, IOException{ HttpClient httpClient = HttpClientBuilder.create().build(); String sysdevEndPoint = "https://devicesigningservice.cloudapp.net/api/signing/devices"; HttpPost request = new HttpPost(sysdevEndPoint); StringEntity params =new StringEntity("{\"TestHarnessType\": \"HLK\",\"OSSelections\": [{\"OS\",\"Windows 7\"},{\"OS\",\"Windows 8\"},{\"OS\",\"Windows 8.1\"},{\"OS\",\"Windows 10\"}],\"ProductName\":\"Test Name\",\"InitialUploadFileSize\": <size>}"); request.setHeader("Content-Type", "application/json"); System.out.println("Sending token ---->"+ access_token); request.setHeader("Authorization: Bearer ", access_token); Log.d("DEBUG", "HEADERS: " + request.getFirstHeader("Authorization: Bearer")); request.setEntity(params); System.out.println("sending Request-------------------------------------------------------------------------->"); HttpResponse response = httpClient.execute(request); System.out.println("-------------------------------------------------------------------------------------------------------"); System.out.println(response); System.out.println("-------------------------------------------------------------------------------------------------------"); Header[] headers = response.getAllHeaders(); for (Header header : headers) { System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue()); } 运行test2时的控制台。为什么呢?

2 个答案:

答案 0 :(得分:1)

你的test2.js正在运行的竞争条件和文件onload事件会在很晚之后触发。

您可以延迟警报,直到设置变量

(function(){
   if (typeof greeting == "undefined")
      return window.setTimeout(arguments.callee, 10);

   alert(greeting.alert);
}());

如果你没有回忆起randomGreeting,你可以内联它

var greet = new Array("BORING JS TEST ALERT", "EXCITING JS TEST ALERT", "AWESOME JS TEST ALERT");
var randGreet = Math.floor(Math.random() * greet.length);
var greeting = {
    alert: greet[randGreet]
};

或者您可以更改脚本以使用defer属性

<script scr="test1.js"></script>
<script scr="test2.js" defer></script>
<script>
window.onload = function(){
    alert(greeting.alert);
};
</script>

答案 1 :(得分:0)

使用立即调用功能:

您可以将randomGreeting()包装在一个立即调用的函数代码块中,以便在声明后立即执行它,从而使您的代码能够正常工作:

var greeting;

(function randomGreeting(){
    var greet = new Array("BORING JS TEST ALERT", "EXCITING JS TEST ALERT", "AWESOME JS TEST ALERT");
    var randGreet = Math.floor(Math.random() * greet.length);

    greeting = {
        alert: greet[randGreet]
    };
})();

在单独的test2.js文件中:

alert(greeting.alert);