I am new to jQuery workflow and I would like to set up a javascript class that uses an internal method to create an AJAX request. When the request returns with success, the jQuery AJAX callback must invoke a method that belongs to the class itself. What code:
function IXClock() { this.m_intervalID = 0; this.startClock = function () { this.m_intervalID = setInterval(this.tictac, 500); } this.stopClock = function () { clearInterval(this.m_intervalID); } this.setClockTime = function(p_strTime) { $('#clock').html(p_strTime); } this.tictac = function () { $.ajax ({ type: 'POST', url: '/rap/rapClock.php', complete: function (data) { this.setClockTime(data); } }); } }
The class is a clock with an internal method (tictac) that asks for "what time" on the server side. After the server talks about time, the jQuery AJAX method should call the setClockTime method of the IXClock class. The invoke method will update the #clock div element in the html page.
The problem is that this.setClockTime () method is showing unknown, and javascript returns the error "this.setClockTime is not a function".
The question is: is there a way to invoka a class method from a jQuery AJAX callback?
iakko source share