【问题标题】:Maintaining a Queue in JavaScript在 JavaScript 中维护队列
【发布时间】:2013-04-22 13:57:10
【问题描述】:

我正在使用 MVC4 并返回一个对象列表以查看为 Json。实际上 View 对 Controller 进行 Ajax 调用以检索数据。我需要维护该对象的队列并将它们显示在我的页面上,这样每个对象将显示 10 秒,然后它将被队列中的第二个对象替换。我正在使用以下代码进行 Ajax 调用

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
});

目前我正在使用按钮单击来刷新它。谁能建议我如何维护返回数据的队列?

【问题讨论】:

  • array.push()array.shift()

标签: javascript jquery asp.net-mvc data-structures


【解决方案1】:

试试这个...

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() 返回数组中的第一项并同时删除它。

【讨论】:

  • 在 -1 处大声笑,没有理由这样做。这正是我们所要求的。
  • 我没有对-1做任何事情?也许在查看您的代码时,它以某种方式被点击了
  • 别担心,伙计——我不认为是你。上周我因不同意某人而惹恼了他们,从那以后他们一直在连续对我投反对票。无论如何,管理员正在奖励我积分。
猜你喜欢
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 2011-04-09
相关资源
最近更新 更多