0

After getting some data using ajax & json i've got this function

    if(data.length != 0) {
        var arreglo = new Array();
        $.each(data, function(index, data) {
            if (jQuery.inArray(data.id, arreglo) == -1) {
                arreglo.push(data.id);
                $("#envivo").append("<li>Titulo: " + data.titulo + " Link: " + data.link + " Fuente: " + data.fuente + "</li>");
            }
        });
    }
}
$(document).ready(function() {
    var fecha = Math.round((new Date()).getTime() / 1000);
    setInterval(function() {
        $.ajax({
            data: "fecha="+fecha,
            type: "GET",
            dataType: "json",
            url: "data.php",
            success: function(data){
                restults(data);
            }
        });
    }, 5000);
});

What i'm trying to do

  1. Check if any data it's retrieved from ajax
  2. Create an array to store data.id
  3. Loop inside the data from ajax
  4. Check if data.id it's in array created in [2]
  5. If id it's not in array, i save it into and apped some data

step 4 it's not working and part of the 5 (saving into array)

Any ideas?

Thanks in advance

2
  • Any error messages? What is the inArray method returning? Commented Apr 13, 2011 at 14:32
  • Nope, any error messages... It keeps appending again and again so i think the array check it's wrong A've added my whole js. Commented Apr 13, 2011 at 14:55

2 Answers 2

1

The inArray function uses the identity compare operator (===).

This will cause problems if the data returned the id as a string, but you are interpreting is as a number.

"9" ==== 9 <- False

"9" == 9 <- True

Sign up to request clarification or add additional context in comments.

11 Comments

I'm using the inArray to check if the data.id it's on the array... is it wrong to use == -1 ?
That part is correct, but in the data sample you provided the id is actually "17", not 17. the inArray function will not match 17 and "17" as they are not the same data type. Either send the data as a number, or loop thru the data that was returned and parseInt to force it to the correct data type.
Hi paul. How can i send the data fine? i'm using json encode... take a look at data.php $fecha = $_GET['fecha']; $feed = array(); $result = mysql_query("SELECT * FROM envivo WHERE fecha >= '".$fecha."' ORDER BY fecha DESC"); $existe = mysql_num_rows($result); if ($existe != 0) { while ($data = mysql_fetch_array($result)) { $jsondata = array(); $jsondata['id'] = $data['id']; $jsondata['titulo'] = $data['titulo']; $jsondata['link'] = $data['link']; $jsondata['fuente'] = $data['fuente']; $feed[] = $jsondata; } echo json_encode($feed); } Thanks again
That should not be a problem, as the values added to the array is the same type as the values used to search in the array.
So where it's the problem with the id?
|
0

I have tried your code, and it works just fine:

http://jsfiddle.net/Jfq6d/

You have to check that the data that you get actually looks the way that you expect. You can for example use the Net tab in the FireBug addon in Firefox to examine the response that you get in the AJAX call.

Edit:

As you want to reuse the function, you have to create the array outside the function:

var arreglo = [];

function restults(data) {
  if(data.length != 0) {
    $.each(data, function(index, data) {
      if (jQuery.inArray(data.id, arreglo) == -1) {
        arreglo.push(data.id);
        $("#envivo").append("<li>Titulo: " + data.titulo + " Link: " + data.link + " Fuente: " + data.fuente + "</li>");
      }
    });
  }
}

6 Comments

Data it's like this [{"id":"17","titulo":"Radio LocalStrike tiene un nuevo programa, Ranking Fusa","link":"http:\/\/www.localstrike.net\/radio\/noticias-55\/radio_localstrike_tiene_un_nuevo_programa_ranking_fusa-1283","fuente":"LocalStrike"}]
@Zuker: I tried with data like that, and it still works: jsfiddle.net/Jfq6d/1 The conclusion is that the data is actually not like that, and you have to check what the data really looks like.
Guffa please try to replicate that using the full code with the setInterval so you can certainly check that id's aren replicating
@Zuker: The method doesn't add any duplicates, but if you run it more than once it will not keep from adding items that was added previously. If you want it to remember the id's from one execution to the next, you have to create the array outside the function.
@Guffa: One problem with the inArray method is that it runs the native indexOf function if available in your browser. That function may not use the === operator and so the comparison function ends up browser specific.
|

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.