weixin_33739646 2019-07-10 09:10 采纳率: 0%
浏览 16

jquery.Ajax数据获取

I try to obtain data stored in another url. There is a text field in which I want to set the value retrieved from the url at every 1 second(The url's content is continually changing at each second). How can I do that? I don't want refreshing entire page at each second. Instead, only the field must be refreshed. But, in my code, the field is not set as intended and it is empty.

What's wrong with my idea/approach?

Html,

<div class="form-group">
<label class="" for="temperature">Temp.:</label>
<input type="text" id="temperature" class="form-control">
</div>

My ajax/jquery,

$(document).ready(function() {
    setInterval(function() {
        $.ajax('/theURLData', {
            success: function(data, status, xhr) {
                $('#temperature').val(data);
            }
        });
    }, 1000);
});
  • 写回答

1条回答 默认 最新

  • weixin_33725239 2019-07-10 09:27
    关注

    I don’t think setInterval(function()… works as intended here. Try the following,

    <script type = "text/javascript"> 
      $(document).ready(function f() {
        $.ajax('/theURLData', {
            success: function(data, status, xhr) {
                $('#temperature').val(data);
                setTimeout(f, 1000);
            }
        });
    }); 
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!