2013-03-15 42 views
1

我有一个简单的JavaScript我用来从脚本中使用AJAX提供内容的DIV。只有在用户点击一个按钮后才会发生这种情况。我希望用户按下按钮(一次)和DIV后什么充满了脚本的德输出:自动刷新按下按钮后使用AJAX和FORM的时间间隔DIV

  1. 禁用按钮
  2. 有DIV重新加载脚本的输出每5秒使用AJAX

    < SCRIPT LANGUAGE = “的JavaScript”>

function xmlhttpPost(strURL) { 
     var xmlHttpReq = false; 
     var self = this; 
     // Mozilla/Safari 
     if (window.XMLHttpRequest) { 
      self.xmlHttpReq = new XMLHttpRequest(); 
     } 
     // IE 
     else if (window.ActiveXObject) { 
      self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     self.xmlHttpReq.open('POST', strURL, true); 
     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
     self.xmlHttpReq.onreadystatechange = function() { 
      if (self.xmlHttpReq.readyState == 4) { 
       updatepage(self.xmlHttpReq.responseText); 
      } 
     } 
     self.xmlHttpReq.send(getquerystring()); 
    } 
function getquerystring() { 
     var form = document.forms['f1']; 
     var xword = form.xword.value; 
     qstr = 'addpost=' + escape(xword); // NOTE: no '?' before querystring 
     return qstr; 
    } 

    function updatepage(str){ 
     document.getElementById("result").innerHTML = str; 
    } 
    </script> 
    <form name="f1"> 
     <input name="xword" type="hidden" value="someword"> 
     <input value="betaling gereed" type="button" name="btn" onclick='JavaScript:xmlhttpPost("script.php")'></p> 
     <div id="result"></div> 
    </form> 
+0

我不明白,使代码显示为代码的方式。它只是没有用。现在我不能再改变它了,因为扎克是第一个。对不起,这个 – user2175718 2013-03-15 22:42:28

+0

我认为它现在是可读的......叹气.. – user2175718 2013-03-15 22:47:36

回答

0

使用setTimeout() JavaScript方法如下:

setTimeout ('xmlhttpPost("script.php")', 5000); 

在你updatepage()方法的末尾,调用此。

例如:

function updatepage(str){ 
    document.getElementById("result").innerHTML = str; 
    setTimeout ('xmlhttpPost("script.php")', 5000); 
    } 

要禁用按钮,您将需要一个id属性添加到您的按钮标签。然后,你可以禁用像这样:

document.getElementById('btn').disable(); 
+0

你太棒了!谢谢! – user2175718 2013-03-15 22:53:41

+0

点击后禁用按钮的任何提示? ;-) – user2175718 2013-03-15 22:55:04

+0

onclick =“this.disabled = true”does not work :-( – user2175718 2013-03-15 22:57:53

0

您正在寻找JS时间事件?

w3shools jvascript time events

onclick="setInterval(func(), 5000)" 

OK编辑:

我没有测试它,是已故

<input id="loadStuffButton" value="betaling gereed" type="button" name="btn" onclick='setInterval(xmlhttpPost("script.php"), 5000);'></p> 

function xmlhttpPost(strURL) { 
    document.getElementById('loadStuffButton').disabled = true; 
.... 
} 

编辑2:测试此 快速和肮脏的不阿贾克斯显示如何setIntervalworks 5毫秒hehe

<form name="f1"> 
    <input name="xword" type="hidden" value="someword"> 
    <input id="loadStuffButton" value="betaling gereed" type="button" name="btn" onclick='setInterval(function(){xmlhttpPost("script.php")}, 500);'></p> 
    <div id="result"> 
     test 

    </div> 
</form> 
<script> 
    i = 1; 
    function xmlhttpPost(strURL) { 
     document.getElementById('loadStuffButton').disabled = true; 
     updatepage(i++); 
    } 

    function updatepage(str){ 
     document.getElementById("result").innerHTML = str; 

    } 
</script> 
+0

我不确定。我已经尝试与settimeout()结合使用onload,但它不断刷新整个页面,而不是div。 – user2175718 2013-03-15 22:51:29

+0

谢谢你的努力,但现在一切都好了。我可以问另一个问题吗? – user2175718 2013-03-15 23:07:55

0

无限重复的Ajax调用:

var refresh = false, 
    btn = document.getElementById("btn"); 

btn.onclick = function(){ 
    refresh = true; 
    this.disable(); 
    fetchData("script.php"); 
} 

function fetchData(strURL){ 
    while(refresh){ 
     setTimeout(xmlhttpPost(strURL), 5000); 
    } 
} 

和:

<form name="f1"> 
    <input name="xword" type="hidden" value="someword"> 
    <input value="betaling gereed" type="button" id="btn" name="btn"></p> 
    <div id="result"></div> 
</form>