0

我有一个带有函数的javascript代码:

function convertTime()
function loadZive()

假设我有以下代码:

function convertTime(){
    alert(exampleVariable);
    };
function loadZive(){
    var exampleVariable = 3;
    convertTime();
    };

看来我的convertTime()功能无法访问exampleVariable。有什么解决方法吗?

这是完整的代码:

function convertTime(publishedDate){
    alert(publishedDate);
    var date = publishedDate;
    var publishedDay = date.toLocaleDateString();
    var publishedHour = date.getHours();            //nemozno pouzit toLocaleTimeString (cielovy format ma byt bez sekund)
    if (publishedHour < 10){
        publishedHour = "0" + publishedHour;
    };
    var publishedMinute = date.getMinutes();        //nacitanie hodin a minut
    if (publishedMinute < 10){
        publishedMinute = "0" + publishedMinute;
    };
    var publishedTime = publishedHour +":"+ publishedMinute;    //ich nasledne spojenie do casoveho formatu HH:MM
    var publishedDateConverted = publishedDay +", "+ publishedTime;
};
function loadZive(publishedDateConverted){
google.load("feeds", "1");  
    function initialize() {
        var feed = new google.feeds.Feed("http://www.zive.sk/rss/sc-47/default.aspx");
        feed.setNumEntries(window.localStorage.getItem("entriesNumber"));
        feed.load(function(result) {
            if (!result.error) {
                var feedlist = document.getElementById("feedZive");
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var li = document.createElement("li");
                    var entry = result.feed.entries[i];
                    var A = document.createElement("A");
                    var descriptionSettings = window.localStorage.getItem("descriptionSettings");
                    if (descriptionSettings=="true"){
                        var h3 = document.createElement("h3");
                        var p = document.createElement("p");
                        var pDate = document.createElement("p");
                        pDate.setAttribute("class","ui-li-aside");
                        var publishedDate = new Date(entry.publishedDate);
                        convertTime();
                        pDate.appendChild(document.createTextNode(publishedDateConverted));
                        h3.appendChild(document.createTextNode(entry.title));
                        p.appendChild(document.createTextNode(entry.content));
                        A.setAttribute("href",entry.link);
                        A.appendChild(h3);
                        A.appendChild(p);
                        A.appendChild(pDate);
                        }
                    else{
                        A.setAttribute("href",entry.link);
                        A.appendChild(document.createTextNode(entry.title));
                    };
                    li.appendChild(A);
                    feedlist.appendChild(li);
                }
                $("#feedZive").listview("refresh");
            }
        });
    }
    google.setOnLoadCallback(initialize);   
};

警报结果未定义。 PSexampleVariable不是用户定义的;它是从提要加载的,所以我不能在convertTime()函数之前定义它。

4

2 回答 2

3

正如 Danny 所说,或者您也可以通过这种方式传递变量:

function convertTime(exampleVariable){
    alert(exampleVariable);
};
function loadZive(){
    var exampleVariable = 3;
    convertTime(exampleVariable);
};
于 2012-09-13T07:08:29.730 回答
1

var从中删除loadZive(),这会使变量loadZive()仅本地化。

或者,exampleVariable作为参数传递给convertTime().

于 2012-09-13T07:07:27.783 回答