views:

39

answers:

2

hi,

I have a continous ajax request asking for some data
that is to be displayed in textarea.
Now my problem, how do I display the data as soon as page is loaded.


My template code is as follows:




     <html>
<head>
<script type="text/javascript" src="/jquerycall/"></script>
<script type="text/javascript">
     $(document).ready(function()
     {
               var setid = 0;
               var s = new String();
               var my_array = new Array();
               var no_of_lines = 0;
               var array_copy = new Array();
               var fname ="{{fname}}"
               var pid = "{{pid}}"
               function displayfile()
               {

                var text = $("#textid").val();
                if(text == "")
                {
                         no_of_lines = 0;
                }
                else
                {

                        array_copy = text.split("\n");
                        no_of_lines = array_copy.length;


                }
                /*$("#tid").val(no_of_lines)*/
                $.ajax({
                                type:"POST",
                                url:"/displayoutput/",
                                datatype:"json",
                                data:{text:no_of_lines,filename:fname,pid:pid},
                                success:function(data)
                                        {

                                                s = data.filedata;
                                                if(s == "")
                                                {
                                                   clearInterval(setid);

                                                }
                                                my_array = s.split("\n");
                                                displaydata(my_array);

                                        }


                        });


              }

                    function displaydata(my_array)
              {

                   var i = 0;
                   var length = my_array.length;
                   for(i=0;i<my_array.length;i++)
                   {
                       var line = my_array[i] + "\n";
                       line = line.replace(/</g,"&lt;");
                       $("#textid").append(line);
                       $("#tid").val(line)

                   }


             }


              $("form").submit(function(event)
              {

                    setid = setInterval(displayfile,1000);
                    event.preventDefault();

              });

              $("#stop").click(function()
              {
                    clearInterval(setid);
              });
     });

</script>
</head>
<body>
<form method="post">
<input type="submit" value="Click Me"/><br><br>
<button type="button" id="stop">Stop</button><br><br>
<textarea id="textid" disabled="true" readonly="true" rows=30 cols=100></textarea><br><br>


</form>
</body>
</html>



Here, the data is loaded when I click the "Click Me"
But I want the data to be loaded as soon as page is loaded
Any suggestions will be valuable
Thanks in advance 

tazim

A: 

Just call displayfile(); within your .ready() handler.

If you don't wait to lose one second, modify your code like this:

$("form").submit(function(event)
{
       setid = setInterval(displayfile,1000);
       event.preventDefault();
}).submit();

Or.. just put displayfile(); anywhere in your ´.readyhandler to execute that ajax request as soon as possible (after theDOM` is ready);

If you don't need the DOM to be ready you can execute that function even further by creating a selfexecuting anonymous function.

(function(){
    // code to execute
})();

You can fill that with the code from your displayfile() function and it will execute asap.

jAndy
please explain it more specifically
tazim
@tazim: see update
jAndy
I am not suppose to use this submit button also.continous ajax request is sent as soon as page is loaded andresponse should be shown in the textarea.The page should contain only textarea . where requests are sent in every one second and data is loaded the interval is cleared as soon as no more data is left. I tried putting submit button just for understanding But ideally the page should contain only textarea with data getting displayed . Thanks in advance for any suggestions sample code
tazim
A: 

tazim,

change:

          $("form").submit(function(event)
          {
                setid = setInterval(displayfile,1000);
                event.preventDefault();
          });

to:

          $(document).ready(function() {
                displayfile();
                setid = setInterval(displayfile,1000);
          });

jim

jim