Adham
Adham

Reputation: 64844

how to pass a parameter to ajaxed loop script?

This function get a parameter and pass it to this this

$.arte({'ajax_url':'getRealTimeUpdates.php?activity_id='+id, 'on_success':updateLiveUpdates}).start();

[where this line of code feches the upadtes from server each a specific time period as a jaxed loop]

when I call the function showLiveUpdates(id) with some parameter for example id=5, this line remember id=5 for all this function calls even different parameters ! I want each time I call the function with different id this line of code get the new id

{{ where arte is JQuery plug-in : Ajax Real Time Extension, this is its website click here}}

js code :

function showLiveUpdates(id)
{
    //$.arte().stop();

    $("#liveUpdates").fadeIn("slow");
    $("#liveUpdates").html("");
    $("#liveUpdates").append("<div><textarea rows='2' cols='49' id='txtLiveUpdates'></textarea></div>");
    $("#liveUpdates").append("<div><input type='button' id='btnliveUpdatesShare' value='share' onClick='addComment("+id+","+getCookie("userId")+")'></div>");
    last="";
    $.arte({'ajax_url':'getRealTimeUpdates.php?activity_id='+id, 'on_success':updateLiveUpdates}).start();

}

I call it like this:

<input type='button' onClick='showLiveUpdates(<? echo $_GET["id"];?>)' />

edit

function updateLiveUpdates(data)
{
    if(data!=last)
    {
        $("#liveUpdates").append("<div id='updates"+ii+"' style='display:none' >"+data+"</div>");
        $("#updates"+ii).fadeIn();
        last=data;
    }
    ii++;
}

getRealTimeUpdates.php

<?php  

require("database.php");
$activity_id=$_GET["activity_id"];
$query = "SELECT id,comment from comments where activity_id=$activity_id order by id desc limit 0,1";
echo $query;
$result = mysql_query($query);
$row = @mysql_fetch_assoc($result);
echo $row["id"]."-".$row["comment"];

?>

Upvotes: 0

Views: 145

Answers (1)

Paul Grime
Paul Grime

Reputation: 15104

How about this? Creating a function scope for the id variable:

function createData(id) {
    return function () {
        return {
            'ajax_url':'getRealTimeUpdates.php?activity_id='+id,
            'on_success':updateLiveUpdates
        }
    }
}

for (var id = 0; id < 5; i++) {
    $.arte(createData(id)()).start();
}

EDIT 1 Just looked at the code at http://code.google.com/p/arte/source/browse/trunk/jquery-plugin-arte.js.

There is a shared _config property that gets overwritten, and the URL used is always _config['ajax_url'].

EDIT 2 I've posted a demo of this here to demonstrate your issue. So it looks like you cannot call this function multiple times, because the internal state is shared.

EDIT 3 I've updated a jsfiddle with a rough attempt at making this code work as you desire. The demo is here. The new code keeps no state and (seems to) successfully run 5 update loops before terminating them after 3 seconds.

Upvotes: 1

Related Questions