views:

51

answers:

1

I have the following code that's checking a database every second and pulling the most recent date stamp and comparing it to the previous one. (or that's what I want), the idea is that it will display that an update has occurred to the end user in an ajax fashion.

Here's the code:

            var old_bid = [];
        var startBidPoll = function() {

                $('#live_auctions').children(/[id_]/).each(
                    function(){

                        //Get Auction Id's
                        var id = $(this).attr('id').replace('id_','');

                        if(old_bid.length < 1){
                            old_bid[id] = new Array();
                        }
                        $.post('getbids.php?auction_id='+id, function(data) {


                            var bid_dt = data.bid_dt;
                            var user_id = data.user_id;
                            //$('#results2').append(old_bid[id][0] + '--' + old_bid[id][1] + '--' + bid_dt + '<br/>');
                            if(id == old_bid[id][0] && bid_dt !== old_bid[id][1]){

                                $('#timer_'+id).effect("highlight", {color: 'red'}, 1000);

                            }
                            if(old_bid.length < 1){
                                old_bid[id] = new Array(id, bid_dt);    
                            }

                        }, "json");

                    }
                );
        }
        setInterval(startBidPoll, 1000);

I'm creating an array "old_bid", that I'm using to compare the old date old_bid[1] and the auction_id old_bid[id][0] and id.

Ok... I'm kind of confused myself here... basically it's only doing it for the first id... meaning that old_bid[ID] the id is not incrementing or saving.... any ideas?

A: 

Change this:

if(old_bid.length < 1){
    old_bid[id] = new Array();
}

.

To this:

if (typeof old_bid[id]  == "undefined")
{
    old_bid[id] = new Array();
}

.
.

And change this:

if(old_bid.length < 1){
    old_bid[id] = new Array(id, bid_dt);
}

.

To this:

if (old_bid[id].length < 1)
{
    old_bid[id] = [id, bid_dt];
}
Brock Adams