PDA

View Full Version : setTimeout error



Lamped
Jul 5th, 2009, 02:21 PM
The main page calls var jc = new jchat('jchat'); and the script goes fine, alerting "Tick".

Works:
46: this._updateTimer = setTimeout(this._updateTick, this._updateInterval);

Doesn't work:
35: this._updateErrorTimer = setTimeout(this._updateError, this._updateErrorTimeout);

Their declarations and use are virtually identical, but even if I wrap this._updateError in quotes as "this._updateError();" it says the function doesn't exist.

I'm pretty new to Javascript, but this makes absolutely no sense to me...


function jchat(id) {
this._version = '0.1-alpha';
this._updateInterval = 3000;
this._updateErrorTimeout = 8000;


this._div = $('#'+id);
this._div.empty();
this._div.addClass('jchat');
this._div.text('Hello World');

this._updateStart();
}


// *************** UTILITY / INFORMATION ***************

jchat.prototype.version = function() {
return this._version;
}


// *************** UPDATE TIMERS ***************

jchat.prototype._updateError = function() {
/*errorDiv = document.createElement('div');
$(errorDiv).text('The connection with the chat server was lost. Please check your connection and refresh the page to try again.');
$(errorDiv).addClass('jchat_errordialog');
this._div.append(errorDiv); */
alert('Update');
}

jchat.prototype._updateTick = function() {
alert('Tick');
this._updateErrorTimer = setTimeout(this._updateError, this._updateErrorTimeout);
$.get('libs/jchat/ajax.php', {user: this._userId, key: this._userKey, type: 'getUpdates' }, function(json) {
clearTimeout(this._updateErrorTimer);
// Process JSON data
// Restart timer
this._updateTimer = setTimeout(this._updateTick, this._updateInterval);
});
}

jchat.prototype._updateStart = function() {
// Use rolling timeout because interval can cause overlaps with ajax lag.
this._updateTimer = setTimeout(this._updateTick, this._updateInterval);
}

jchat.prototype._updateStop = function() {
clearTimeout(this._updateTimer);
}

vwphillips
Jul 5th, 2009, 02:42 PM
try


this._updateErrorTimer=setTimeout(function(oop){return function(){oop._updateError();}}(this),this._updateErrorTimeout);

Lamped
Jul 5th, 2009, 02:50 PM
oop._updateError is not a function, apparently :confused:

vwphillips
Jul 5th, 2009, 03:09 PM
alert(this._updateError);


should display the function code, if not try changing the name

Lamped
Jul 5th, 2009, 03:22 PM
alert(this._updateError);


should display the function code, if not try changing the name

It works if I put it in the constructor (function jquery()) but not if I put it in any of the prototype functions.

Lamped
Jul 5th, 2009, 03:43 PM
I read "When setTimeout evaluates a code as the first parameter, it has no reference to "this" being the Timer. So you'll have to give it a Timer instance to run on." on another forum. With this in mind I gave all functions an obj parameter, and converted them all to work on obj, not this. It seems to work now.

vwphillips
Jul 5th, 2009, 04:55 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
function jchat(id) {
this._version = '0.1-alpha';
this._updateInterval = 3000;
this._updateErrorTimeout = 8000;

alert(this._updateError);
this._updateTick()
// this._div = $('#'+id);
// this._div.empty();
// this._div.addClass('jchat');
// this._div.text('Hello World');

// this._updateStart();
}


// *************** UTILITY / INFORMATION ***************

jchat.prototype.version = function() {
return this._version;
}


// *************** UPDATE TIMERS ***************

jchat.prototype._updateError = function() {
/*errorDiv = document.createElement('div');
$(errorDiv).text('The connection with the chat server was lost. Please check your connection and refresh the page to try again.');
$(errorDiv).addClass('jchat_errordialog');
this._div.append(errorDiv); */
alert('Update');
}

jchat.prototype._updateTick = function() {
alert(this._updateError);
this._updateErrorTimer = setTimeout(this._updateError,10);

return
this._updateErrorTimer=setTimeout(function(oop){return function(){oop._updateError();}}(this),this._updateErrorTimeout);
$.get('libs/jchat/ajax.php', {user: this._userId, key: this._userKey, type: 'getUpdates' }, function(json) {
clearTimeout(this._updateErrorTimer);
// Process JSON data
// Restart timer
this._updateTimer = setTimeout(this._updateTick, this._updateInterval);
});
}

jchat.prototype._updateStart = function() {
// Use rolling timeout because interval can cause overlaps with ajax lag.
this._updateTimer = setTimeout(this._updateTick, this._updateInterval);
}

jchat.prototype._updateStop = function() {
clearTimeout(this._updateTimer);
}
/*]]>*/
</script></head>


<body onload="S=new jchat()">

</body>

</html>

you problem is elsewhere

Lamped
Jul 5th, 2009, 06:05 PM
you problem is elsewhere

I'm inclined to agree, I'm kinda hoping someone can suggest where and what.