简体   繁体   English

函数总是返回相同的值

[英]Function always returns the same value

I'm working on an Windows 8 app and using JavaScript for the first time. 我正在使用Windows 8应用程序,并且是第一次使用JavaScript。 I haven't had any big issues until now. 到目前为止,我还没有遇到什么大问题。 The following function makes a request and gets a JSON object. 以下函数发出请求并获取JSON对象。 Comparing the result I get from my browser and from the debugger everything is okay in the first request. 比较我从浏览器和调试器得到的结果,在第一个请求中一切正常。 After that it seems like the server is always returning the same value, which is not the case 之后,服务器似乎总是返回相同的值,事实并非如此

(function () {
    "use strict";


    function getPrice(currency) {
    var url;

    switch(currency) {
        case "usd":
            url = urlUSD;
            break;

        case "eur":
            url = urlEUR;
            break;

        case "gbp":
            url = urlGBP;
            break;
    }

    WinJS.xhr({ url: url }).then(
        function (response) {
            var json = JSON.parse(response.responseText);
            switch(currency) {
                case "usd":
                    console.log(json.data);
                    sharedData.usd = parseFloat(json.data);
                    console.log(sharedData.usd);
                    break;
                case "eur":
                    console.log(json.data);
                    sharedData.eur = parseFloat(json.data);
                    console.log(sharedData.eur);
                    break;
                case "gbp":
                    console.log(json.data);
                    sharedData.gbp = parseFloat(json.data);
                    console.log(sharedData.gbp);
                    break;
            }
        },
        function (error) { console.log(error); },
        function (progress) { }
    );
}

The calling function 调用功能

(function () {
"use strict";

function onTimer() {
    Exchanges.getPrices("usd");
    setTimeout(onTimer, 1000*60);
}

setTimeout(onTimer, 1000);
})();

There are many techniques to handle cache issue. 有许多技术可以处理缓存问题。 I have used the technique of adding an timestamp parameter to the url to fix this in past. 我过去曾使用向网址添加时间戳参数的技术来解决此问题。

var timestamp = Date.now();
url += '?' + 'timestamp=' + timestamp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM