我正在尝试使用ID fixed-button
获取我的按钮上的文字,以便使用此定期更新:
<script type="text/javascript">
$(function()
{
$('#fixed-button').setInterval(function() {
$.getJSON("{{ url('schedulizer/cart') }}", function(data) {
$(this).text(data.quantity);
});
}, 5000);
});
</script>
但是,我收到的JS错误是:
Uncaught TypeError: $(...).setInterval is not a function
问题很明显 - this
属性没有.setInterval
功能。所以问题是 - 如何在常规时间段内使用AJAX在此按钮元素上设置文本?
答案 0 :(得分:2)
setInterval
不是jQuery函数,它是一个javascript函数。
<script type="text/javascript">
$(function()
{
var $this=$('#fixed-button');
window.setInterval(function() {
$.getJSON("{{ url('schedulizer/cart') }}", function(data) {
$this.text(data.quantity);
});
}, 5000);
});
</script>
DOC: setInterval