2013-04-22 64 views
3

我的工作和MVC4並返回對象列表以查看JSON。實際上,View會對Controller進行Ajax調用以檢索數據。我需要維護該對象的隊列並在我的頁面上顯示它們,以便每個對象都將顯示10秒,然後它將被替換爲隊列中的第二個對象。我使用下面的代碼,以使一個Ajax調用保持隊列中的JavaScript

function GetData() { 
    $.get("http://localhost:45533/Home/GetData/", function (data) { 
     ProcessData(data); 
     // Here i need to add [data] to Queue 
    }); 
} 

function ProcessData(data) { 
    $("#myDiv").append(data.Name+ "<br/>"); 
} 

$("#fetchBtn").click(function() { 
    // Here i need to get the next object in data from Queue 
}); 

目前我使用按鈕,點擊刷新。任何人都可以建議我如何維護返回的數據隊列?

+2

看看'array.push()'和'array.shift()' – Archer 2013-04-22 14:01:18

回答

0

嘗試......

var arData = []; 

function GetData() { 
    $.get("http://localhost:45533/Home/GetData/", function (data) { 
     ProcessData(data); 
     // Here i need to add [data] to Queue 
    }); 
} 

function ProcessData(data) { 
    arData.push(data) // add it to the end of the array 
    $("#myDiv").append(data.Name+ "<br/>"); 
} 

$("#fetchBtn").click(function() { 
    // Here i need to get the next object in data from Queue 
    var data = arData.shift(); // get the first item of the array 
}); 

arData.push(data)增加data到數組的末尾,而arData.shift()返回數組中的第一項,並在同一時間刪除。

+0

Lol在-1沒有理由。這正是要求的。 – Archer 2013-04-22 14:07:18

+0

我還沒有做過什麼-1?也許在看你的代碼的時候,它不知何故被點擊 – InTheWorldOfCodingApplications 2013-04-22 14:28:58

+0

不要擔心隊友 - 我不認爲這是你。我上週因與他們不同意而對某人感到不滿,自那以後,他們一直在低調地對我表示不滿。無論如何,Admin都會給我獎勵點數。 – Archer 2013-04-22 14:42:50