I use a function like this. You can adjust the parameters to suit, like pass in an element rather than and id:
function getTotalCount(entitySet, elemId) {
entitySet
.top(1)
.includeTotalCount()
.execute()
.then(function (result1) {
// update the total count
document.getElementById(elemId).innerText = result1.totalCount.toString() + " rows";
}, function (error) {
// do whatever you want to
totalProperty = "error";
});
}
Dave