iak*_*kko 5 javascript methods ajax jquery class
我是关于jQuery工作流程的新手,我想设置一个使用内部方法发出AJAX请求的javascript类.当请求成功返回时,jQuery AJAX回调应该调用类本身拥有的方法.那是代码:
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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
该类表示一个时钟,内部方法(tictac)请求服务器端的"时间".在服务器说出时间之后,jQuery的AJAX方法应该调用IXClock类的setClockTime方法.invoke方法将更新html页面中的#clock div项.
问题是方法this.setClockTime()结果未知,javascript返回"this.setClockTime不是函数"错误.
问题是:有没有办法从jQuery的AJAX回调中调用类方法?
Gus*_*Gus 11
我认为问题在于this
你的回调函数与this
引用不同IXClock
.尝试:
var thisClass = this ;
this.tictac = function ()
{
$.ajax
({
type: 'POST',
url: '/rap/rapClock.php',
complete: function (data)
{
thisClass.setClockTime(data);
}
});
}
Run Code Online (Sandbox Code Playgroud)
测试用例(添加到已加载jQuery的站点):
function uClass () {
this.testFunction = function(input) {
alert(input) ;
}
this.ajaxFunction = function() {
var myClass = this ;
$.ajax({
type: 'POST',
url: '/',
complete: function(data) {
alert(myClass.testFunction) ;
myClass.testFunction(data) ;
this.testFunction(data) ;
}
}) ;
}
}
var k = new uClass() ;
k.ajaxFunction() ;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9263 次 |
最近记录: |