我有一個連續的ajax請求,要求輸入一些要在textarea中顯示的數據 。 現在我的問題是,如何在頁面加載後立即顯示數據。加載頁面後在textarea中顯示ajax響應
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,"<");
$("#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
請解釋更具體 – tazim 2010-06-24 06:41:10
@tazim:看到更新 – jAndy 2010-06-24 06:58:50
我不是想用這個提交按鈕也。 加載頁面後會立即發送連續的ajax請求,並且 響應應顯示在textarea中。 頁面應該只包含textarea。每隔一秒發送一次請求並加載數據,只要沒有剩下數據,間隔就會被清除。我試着把提交按鈕只是爲了理解,但理想情況下,頁面應該只包含textarea,並顯示數據。預先感謝任何建議示例代碼 – tazim 2010-06-24 07:25:21