2011-11-18 65 views
2

我正在使用jQuery AJAX调用JSON服务。然后我将数据吐出到.html。我想要2件事情发生。 1.我想刷新按钮来刷新数据,而不是整个页面。 2.我想每隔5分钟左右更新一次setTimeout或setInterval(哪个效果最好)。但刷新页面。 如何将AJAX包装在setTimeout或setInterval中,或每隔5分钟左右使用按钮和计时器刷新数据。我知道这应该很简单,但我一直没有能够得到它的工作。提前致谢。jQuery Ajax setTimeout JSON

以下是我的代码。

$.ajax({ 

    type: "POST", 
    url: "/myservice.asmx/WebMethod", 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (msg) { 
     var myval = msg.d; 
     // $('#jsonstring').html(myval); 
     var obj = jQuery.parseJSON(myval); 
     $('#Data1').html(obj.DataOne); 
     $('#Data2').html(obj.DataTwo); 

    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
     alert(xhr.status); 
     alert(thrownError); 
    } 
}); 
+0

没关系我想通了自动重装,并点击该按钮对不起无关的问题 – ClosDesign

回答

0
function rData(){ 
    $.ajax({ 

     type: "POST", 
     url: "/myservice.asmx/WebMethod", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      var myval = msg.d; 
      // $('#jsonstring').html(myval); 
      var obj = jQuery.parseJSON(myval); 
      $('#Data1').html(obj.DataOne); 
      $('#Data2').html(obj.DataTwo); 

     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert(xhr.statusText); 
      alert(xhr.responseText); 
      alert(xhr.status); 
      alert(thrownError); 
     } 
    }); 
} 

setInterval(rData,300000); 

<input type='submit' value='refresh' onclick='rData();return false;'> 
0

假设你的Ajax调用的工作,你可以把它放在一个函数和参考,在定时器/间隔。

function Refresh() { 
    /* your ajax code */ 
} 

setInterval(Refresh, 300000); // five minutes, in milliseconds 

$("#MyButton").click(Refresh); 
+0

这是不是只能运行刷新()函数一次,第5分钟之后呢? –

+0

@JakeFeasel不,'setInterval'无限期地重复它,而不是'setTimeout',它只做一次。所以没有必要重新设置你的答案中的超时时间。 – Tesserex

+0

啊 - 很高兴知道。谢谢 –

0
function refreshContent() { 
    $.ajax..... 
} 

function refreshContentTimer() { 
    refreshContent(); 
    setTimeout(refreshContentTimer, 300000); // 5 min 
} 

$(function() { 

    setTimeout(refreshContentTimer, 300000); 
    $("#refreshButton").click(refreshContent); 

} 
1

这就是我所做的。我也从这篇文章中发现,间隔或超时将与数据加载和加载相冲突。所以我必须这样写。

function myFunction() { 
    $.ajax({ 
     type: "POST", 
     url: "/myservice.asmx/WebMethod", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      var myval = msg.d; 
      // $('#jsonstring').html(myval); 
      var obj = jQuery.parseJSON(myval); 
      $('#Data1').html(obj.DataOne); 
      $('#Data2').html(obj.DataTwo); 

      //TO SET THE TIMEOUT FOR DATA TO LOAD 

      setTimeout(function(){ 
       yourFunction(); 
      }, 300000); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert(xhr.statusText); 
      alert(xhr.responseText); 
      alert(xhr.status); 
      alert(thrownError); 
     } 
    }); 
    } 

    $(document).ready(function(){ 
    //Load on Doc Ready 
    myFunction(); 

    //Reload on button click 
    $('#myBtn').click(function(){ 
     myFunction(); 
    }); 

    //TO SET THE TIMEOUT FOR DATA TO LOAD - Need both in the success and here. 

    setTimeout(function(){ 

     myFunction(); 

    }, 2000); 
    });