The problem is you are trying to use PHP within javascript; luckily, javascript provides the same string manipulation functions that you need. The following
should work (I don't know if you explicitly need to declare the string as an object or not, let me know if it isn't working):
Code:
var max_title_length = 30;
for(...) {
if(threads[x].title.length > max_title_length)
{
threads[x].title = threads[x].title.substr(0, max_title_length) + '...';
}
// ... format/output code
}