2017-12-03 161 views
0

我有函數bellow每5秒調用一次從服務器獲取數據,這是flask/python。我的問題是,如何調整getjson調用以在成功檢索數據時進行回調。getJSON done回調

我知道那裏有.done .fail等等,但我想知道如果我可以保留這個結構,只是添加下面的結構,但我不知道在這種情況下的語法,希望這不是太感到困惑,感謝閱讀,這是代碼。

// get data from the server every getDataFromServerInterval milliseconds 
var getDataFromServerInterval = 5000; 
function getData(){ 
    // request timesince table entries from server for user... 
    $.getJSON($SCRIPT_ROOT + '/_database', { 
    action: "getUserTable_timesince", 
    username: $('input[name="username"]').val() 
    }, function(data) { // do something with the response data 
    timesince_dataBuffer = data; 
    }); 
    return false; // prevent get 
} 
// get data from the server every getDataFromServerInterval milliseconds 
setInterval(getData, getDataFromServerInterval); 
+0

你想用getData中的回調做什麼? – Jasjeev

+0

_「保持這種結構,只是添加波紋管它」_:這是不明確的。你什麼意思?你能編輯你的問題來舉個例子嗎? – Andy

回答

0

我發現的部分解決方案,我發現我可以在一個處理所接收的數據的功能,這是有點相當於在不同的getJSON呼叫結構.done的末尾添加一個回調,我還不確定在接收數據之前還是之後調用該函數。

// global timesince buffer, holds 
var timesince_dataBuffer; 

// get data from the server every getDataFromServerInterval milliseconds 
var getDataFromServerInterval = 5000; 
function getData(){ 
    // request timesince table entries from server for user 
    $.getJSON($SCRIPT_ROOT + '/_database', { 
    action: "getUserTable_timesince", 
    username: $('input[name="username"]').val() 
    }, function(data) { // do something with the response data 
    timesince_dataBuffer = data; 
    updateEntryStruct(); // the hope is to call this when data is received 
    }); 
    return false; // prevent get 
} 
// get data from the server every getDataFromServerInterval milliseconds 
setInterval(getData, getDataFromServerInterval); 
0

你可以這樣做。不要處理getData中的數據或使用回調,請利用$.getJSON返回的承諾。有一個單獨的函數,由調用數據的超時調用,then處理它。它整齊地將你的代碼分離成更多可管理的功能。

var getDataFromServerInterval = 5000; 

function getData() { 
    return $.getJSON($SCRIPT_ROOT + '/_database', { 
    action: "getUserTable_timesince", 
    username: $('input[name="username"]').val() 
    } 
} 

function wrangleData() { 
    getData().then(function (data) { 
    console.log(data); 
    }); 
} 

setInterval(wrangleData, getDataFromServerInterval); 
+0

謝謝,不知道這個語法。我想出了這個: var timesince_dataBuffer; ($ SCRIPT_ROOT +'/ _database',{ action:「getUserTable_timesince」, username:$('input [name =「username」]')。val() },function(data){ timesince_dataBuffer = data; updateEntryStruct(); }); 返回false; } 這個調用是更新內容,按鈕等的結構。 裏面的文本是按時間間隔更新的,因爲我使用的是時間對象。這樣我就不會重新創建按鈕,只有文本。 –

+0

對不起,縮進,我不知道如何縮進回覆。 –

0

這是我想出的解決方案。

var timesince_dataBuffer; 
function getData(){ 
    // gets user's entries from sql table 
    $.getJSON($SCRIPT_ROOT + '/_database', { // $SCRIPT_ROOT, root to the application 
    action: "getUserTable_timesince", 
    username: $('input[name="username"]').val() 
    }, function(data) { // if a response is sent, this function is called 
    timesince_dataBuffer = data; 
    updateEntryStruct(); // recreate the structure of each content, buttons etc 
    }); 
    return false; 
} 

我得到的數據,放在一個全局變量,調用另一個函數,該函數接收到的每個對象的數據並重新創建一個結構,這樣一來我不重建它是靜態的結構件,最重要的是鈕釦。

另一個函數每1秒調用一次,它更新動態部分。 (格式化的時間),因爲 (事件名稱)傳送

無論如何,這實際上是我在CS50最後一個項目,我開始通過表單提交的服務器進行通信,每個用戶按下一個按鈕時刷新頁面,則我是用ajax做的,但是我每2秒向服務器發送一次請求,並且沒有響應按鈕,因爲我會在一定的時間間隔內自己重新創建按鈕。 現在,網頁感覺反應迅速,效率很高,這是一次很棒的學習體驗。

如果有人想查看代碼,一切都在這裏。 https://github.com/silvermirai/cs50-final-project

它基本上是一堆隨機功能,想到了。 該應用程序可以在這裏找到截至目前。 http://ide502-silvermirai.cs50.io:8080/