You're mistaking Java for JavaScript. The two are not related at all, despite a similar name.
The PHP templates pick up the current listener count from the SAM database, which only stores that number once for every track played in the historylist. So as soon as the track starts playing the then current listener count is recorded.
If you want a live listener count, you can ask your streamserver for it. Shoutcast v2 can give proper JSON responses for statistics which are easy to parse in JS.
There's just one limitation: If your website is https (lock symbol, certificate, etc.), you can only fetch data from other HTTPS sites using JavaScript. Stream servers usually are NOT providing an HTTPS connection. (Though, it can be configured in Shoutcast 2 or can be wrapped by a reverse proxy)
For example shoutcast 2 reports all the statistics of the server/stream on this address:
<your-server-hostname-and-port>/stats?sid=<your-SID, if you're alone on that server it's usually 1>&json=1
i.e.:
http://144.76.106.52:7000/stats?sid=1&json=1 (Stats page from a fairly popular [~500 listeners across all formats] German ProgHouse Channel called Hirschmilch Radio)
This code will look through your website for an HTML element with the ID stats and put the current number of listeners in there.
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
fetch('http://144.76.106.52:7000/stats?sid=1&json=1').then((res) => {return res.json()}).then((data) => {document.getElementById('stats').innerHTML = data.currentlisteners;})
What it does is 1: fetch the json document from the Shoutcast v2 server., 2: then translate JSON to javascript object, 3: then find an HTML element (i.e. <div>, <span>, etc.) with the id 'stats' (i.e.: <div id="stats">) and set the innerHTML of that element to the number from the Shoutcast stats page. (innerHTML overwrites everything enclosed by the element, i.e.: <div id="stats">Our Listener count is: 1</div> would be overwritten by <div id="stats">99</div> if there were 99 currentListeners)
To make it update regularly every 30 seconds:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
setInterval(() => {fetch('http://144.76.106.52:7000/stats?sid=1&json=1').then((res) => {return res.json()}).then((data) => {document.getElementById('stats').innerHTML = data.currentlisteners;})}, 30000);
Note: Times in JavaScript are always given in milliseconds hence the number 30000 being used here.