1

Yesterday I ask a question about json

Link: How to return an array from jQuery ajax success function and use it in a loop?

And one of the answers was this

setInterval(updateTimestamps,30000);
var ids = new Array();

function updateTimestamps(){
    $(".timestamp").each(function(i){
    var obj = new Object();
    obj.id = $(this).attr("postID");
    obj.timestamp = $(this).attr("postdate");
        ids.push(obj);
    }

    $.post("http://site.com/ajax/humanTime.php", {"time": ids}, function(data) {
        for (i = 0; i < data.length; i++) {
            $("#" + data[i].id).html(data[i].content);
        }
    }, "json");
}

The problem with this script is that the data is dublicated

The first time when this is executed is something like this

Array
(
    [0] => Array
        (
            [id] => 26629
            [timestamp] => 1332273712
        )

    [1] => Array
        (
            [id] => 26628
            [timestamp] => 1332243526
        )

    [2] => Array
        (
            [id] => 26627
            [timestamp] => 1332237777


)

And the second time is

Array
(
    [0] => Array
        (
            [id] => 26629
            [timestamp] => 1332273712
        )

    [1] => Array
        (
            [id] => 26628
            [timestamp] => 1332243526
        )

    [2] => Array
        (
            [id] => 26627
            [timestamp] => 1332237777
        )

    [3] => Array
        (
            [id] => 26629
            [timestamp] => 1332273712
        )

    [4] => Array
        (
            [id] => 26628
            [timestamp] => 1332243526
        )

    [5] => Array
        (
            [id] => 26627
            [timestamp] => 1332237777
        )

)

I try with var ids= Array(); , vas ids = []; but that dosent work

1
  • Did you check what the PHP script itself returns ? Commented Mar 21, 2012 at 7:19

5 Answers 5

3

A short way to reset an array: ids.length = 0;. So

function updateTimestamps(){
  ids.length = 0;
  // [...]
}
Sign up to request clarification or add additional context in comments.

Comments

3

Set ids = [] before you start pushing new items. Here is your code, re-factored:

var ids;

function updateTimestamps() {
    ids = []; // <-- the answer
    $(".timestamp").each(function(i) {
        ids.push({
            id: $(this).attr("postID"),
            timestamp: $(this).attr("postdate")
        });
    });
    $.post("http://site.com/ajax/humanTime.php", {"time": ids}, function(data) {
        for (var i = 0; i < data.length; i++) {
            $("#" + data[i].id).html(data[i].content);
        }
    }, "json");
}

setInterval(updateTimestamps, 30000);​

Comments

2

try this:

var ids;

function updateTimestamps(){
  ids = [];
   ...

Comments

2

After

function updateTimestamps(){

Add

ids = [];

Comments

2
setInterval(updateTimestamps,30000);

function updateTimestamps(){
    var ids = []; // put it here

    $(".timestamp").each(function(i){
    var obj = {};
    obj.id = $(this).attr("postID");
    obj.timestamp = $(this).attr("postdate");
        ids.push(obj);
    }

    $.post("http://site.com/ajax/humanTime.php", {"time": ids}, function(data) {
        for (i = 0; i < data.length; i++) {
            $("#" + data[i].id).html(data[i].content);
        }
    }, "json");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.