0

我有一个按时间间隔设置的函数:

function refresh() {
    if ($("#autorefresh").checked) {
        var elem = $("#messagesDiv");
        elem.scrollTop = elem.scrollHeight;
    }
}

var autoRefresh = window.setInterval(refresh(), 1000);

以及 html 页面上的简单复选框输入。我继续在函数上得到一个 Unexpected Token Function refresh()。我已经检查了太多的逗号,太多的括号,从这个意义上说似乎没有任何问题。有任何想法吗?

4

1 回答 1

3
var autoRefresh = window.setInterval(refresh(), 1000);

needs to be

var autoRefresh = window.setInterval(refresh, 1000);

and you will find out that

$("#autorefresh").checked

needs to be

$("#autorefresh").prop("checked")

and your third problem, there is no scrollTop and scrollHeight properties either, you are NOT working with a DOM element

elem.scrollTop = elem.scrollHeight;

should be something like

elem.scrollTop(elem[0].scrollHeight);  

The updated code would be

function refresh () {
    if ($("#autorefresh").prop("checked")) {
        var elem = $("#messagesDiv");
        elem.scrollTop(elem[0].scrollHeight);
    }
}
var autoRefresh = window.setInterval(refresh, 1000);

Running example:

http://jsfiddle.net/7eEcw/

于 2013-11-01T16:50:06.193 回答