views:

29

answers:

1

Hi,

Written some code in my view function : This code reads a file from server . stores it in a list .passes to client def showfiledata(request):

f = open("/home/tazim/webexample/test.txt")          
   list = f.readlines()       
   return_dict = {'list':list}       
   json = simplejson.dumps(list)       
   return HttpResponse(json,mimetype="application/json")

On, client side the $.ajax callback function receives this list of lines.

Now, My Question is . I have to display these lines in a textarea. But these lines should not be displayed at once . Each line should be appended in textarea with some delay. (Use of setInterval is required as per my knowledge) . Also I am using jquery in my templates.

The server used is Django . Please provide some solution as in some sample code will be quite helpful .

A: 

So, looks like you've got a list of lines from the server, which I assume will serialize to a JavaScript array in the library you're using. If so, then you can just pop lines off the array using setTimeout (which is better than setInterval for most animations).

so, something like this:

// assuming some array named 'lines' holds your lines from the server
function appendLine(){
    var currentValue = $('mytextarea').val();
    var nextLine = lines.shift();
    $('mytextarea').val(currentValue+nextLine);
    if(lines.length > 0)
        setTimeout("appendLine",5000);
}

window.setTimeout("appendLine",5000);  

Probably not the most efficient way to do it, but that's the gist.

Paul