0

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"];

?>
7
  • Are checking that getRealTimeUpdates.php is return something different? Commented Sep 19, 2011 at 20:36
  • yes, but I need to send a parameter to getRealTimeUpdates.php Commented Sep 19, 2011 at 20:38
  • Are you sure that id in the function is different each time? Commented Sep 19, 2011 at 20:44
  • I die a little inside when I see jQuery written along-side obtrusive event bindings. Commented Sep 19, 2011 at 20:44
  • @Adam Terlson sorry, I don't understand "jQuery written along-side obtrusive event bindings" .. what is the problem and possible solution ? Commented Sep 19, 2011 at 20:48

1 Answer 1

1

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.

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

11 Comments

The same thing , I think the problem is in this function '$.arte().start();' .. it remembers value ..
How about edited answer? Added inner function and immediate call of that function from the loop.
function createData(id) { return function () { alert(id); return { 'ajax_url':'getRealTimeUpdates.php?activity_id='+id, 'on_success':updateLiveUpdates } } } alert work fine sends a new id each time ,, but the function retuen the same id !!!
Where's ii coming from? That's going to retain the last value isn't it? You'll probably be better off passing ii and a flag indicating last in the data object.
Check out new edit. I've hacked the arte plugin, but use at your own risk. It seems to work though!! When you try the demo, make sure you have Firebug Net panel open to see the requests being fired.
|

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.