我试图获取存储在另一个url中的数据。有一个文本字段,其中我希望将从url检索到的值设置为每1秒(url的内容在每秒钟不断变化)。我怎么能这么做?我不希望在每一秒刷新整个页面。相反,只有字段必须刷新。但是,在我的代码中,字段不是按预期设置的,而是空的。
我的想法/方法有什么问题?
Html,
<div class="form-group">
<label class="" for="temperature">Temp.:</label>
<input type="text" id="temperature" class="form-control">
</div>我的ajax/jquery
$(document).ready(function() {
setInterval(function() {
$.ajax('/theURLData', {
success: function(data, status, xhr) {
$('#temperature').val(data);
}
});
}, 1000);
});发布于 2019-07-10 09:27:08
我认为setInterval(function()…在这里不像预期的那样工作。尝试以下几点,
<script type = "text/javascript">
$(document).ready(function f() {
$.ajax('/theURLData', {
success: function(data, status, xhr) {
$('#temperature').val(data);
setTimeout(f, 1000);
}
});
});
</script>https://stackoverflow.com/questions/56967288
复制相似问题