将值存储在函数中

时间:2013-12-03 14:21:57

标签: javascript jquery websocket

我有这个功能,通过websocket处理从服务器返回的数据。根据您所在的页面,数据的处理方式不同。

sock.onmessage = function (e) {
    log(e.data);
}

function returnCurrentPage(m) {
    if (helpers.getCurrentPage() === '#builddetail_page') {
        // For the builddetailpage
        var stepList = $('#stepList > li');
        return realtimePages.buildDetail(m, stepList);
    }
    if (helpers.getCurrentPage() === '#builders_page') {
        var tableRowList = $('.tablesorter-js tbody > tr');
        return realtimePages.buildersPage(m, tableRowList);
    }
}

// Data to be parsed
function log(m) {
    returnCurrentPage(m)
};

我的问题是每次调用log()时,returnCurrentPage()也是如此。如何从log()中存储的returnCurrentPage()获取返回值?所以每次log()运行时我都不必运行returnCurrentPage()

2 个答案:

答案 0 :(得分:0)

您可以在localStorage中保存返回的值吗?

sock.onmessage = function (e) {
    log(e.data);
}

function returnCurrentPage(m) {
    if (helpers.getCurrentPage() === '#builddetail_page') {
        // For the builddetailpage
        var stepList = $('#stepList > li');
        return realtimePages.buildDetail(m, stepList);
    }
    if (helpers.getCurrentPage() === '#builders_page') {
        var tableRowList = $('.tablesorter-js tbody > tr');
        return realtimePages.buildersPage(m, tableRowList);
    }
}

// Data to be parsed
function log(m) {
    if( localStorage.returnPage === undefined )
        localStorage.returnPage = returnCurrentPage(m)
};

答案 1 :(得分:0)

您可以存储记录在全局变量中的值,并根据需要清除它。