gpt4 book ai didi

javascript - Javascript 中的变量范围

转载 作者:行者123 更新时间:2023-11-30 06:06:52 25 4
gpt4 key购买 nike

在这种情况下,我无法理解“this”的范围。我可以像这样调用这些函数中的每一个:this.startTracking();从时间跟踪器开关对象中。但是,当我尝试执行代码时:Drupal.timeTracker.prototype.stopTracking();它失去了所有变量范围,我的 GET 请求变得未定义。如何在卸载前关闭 stopTracking()?

    Drupal.behaviors.dynamicTimeTracker = function (context) {
$('form.node-time-tracker', context).each(function () {
new Drupal.timeTracker(this);
});
};

/**
* A time tracker switch object
*/
Drupal.timeTracker = function (form) {
var tracker = this;
this.form = form;
this.nid = $('#'+ form.id +' input[name="nid"]').attr('value');
this.uid = $('#'+ form.id +' input[name="uid"]').attr('value');
this.button = $('#'+ form.id +' input[type="submit"]');
this.url = Drupal.settings.time_tracker.url + '/' + this.nid + '/' + this.uid;
this.counter = $('#'+ form.id +' .counter');

this.initialize(); // TODO: make sure this function is called regularly to make sure trackers are in synch
this.startTracking();
$(window).bind('beforeunload', function() {
Drupal.timeTracker.prototype.stopTracking(); // need help here
});
};

/**
* Initialize the time tracker
*/
Drupal.timeTracker.prototype.initialize = function () {
var tracker = this;

$.ajax({
type: "GET",
url: tracker.url,
dataType: 'json',
success: function (status) {
$(tracker.counter).countdown({compact:true, since:-status['time']}).countdown('resume');

if (status['status'] == 'ongoing') {
$(tracker.button).toggle(
function() {
tracker.stopTracking();
return false;
},
function() {
tracker.startTracking();
return false;
}
);
$(tracker.counter).countdown('resume');
$(tracker.button).val(Drupal.t('Stop'));
$(tracker.form).removeClass('node-time-tracker-start').addClass('node-time-tracker-stop');
}
else {
$(tracker.button).toggle(
function() {
tracker.startTracking();
return false;
},
function() {
tracker.stopTracking();
return false;
}
);
$(tracker.counter).countdown('pause');
$(tracker.button).val(Drupal.t('Start'));
$(tracker.form).removeClass('node-time-tracker-stop').addClass('node-time-tracker-start');
}
},
error: function (xmlhttp) {
alert(Drupal.ahahError(xmlhttp, tracker.startURL));
}
});
};

/**
* Starts time tracking
*/
Drupal.timeTracker.prototype.startTracking = function () {
var tracker = this;

// Ajax GET request for starting time tracker
$.ajax({
type: "GET",
url: tracker.url + '/start',
dataType: 'json',
success: function (status) {
$(tracker.counter).countdown('change', {since: -status['time']}).countdown('resume');
$(tracker.button).val(Drupal.t('Stop'));
$(tracker.form).removeClass('node-time-tracker-start').addClass('node-time-tracker-stop');
},
error: function (xmlhttp) {
alert(Drupal.ahahError(xmlhttp, tracker.startURL));
}
});
};

/**
* Stops time tracking
*/
Drupal.timeTracker.prototype.stopTracking = function () {
var tracker = this;

// Ajax GET request for stopping time tracker
$.ajax({
type: "GET",
url: tracker.url + '/stop',
dataType: 'json',
success: function (status) {
$(tracker.counter).countdown('change', {since: -status['time']}).countdown('pause');
$(tracker.button).val(Drupal.t('Start'));
$(tracker.form).removeClass('node-time-tracker-stop').addClass('node-time-tracker-start');
},
error: function (xmlhttp) {
alert(Drupal.ahahError(xmlhttp, tracker.startURL));
}
});
};

最佳答案

我只是拿出一个小片段:

this.startTracking();
$(window).bind('beforeunload', function() {
// this is defined by the above function definition
tracker.stopTracking();
});

您的问题是,当您创建绑定(bind)函数时,其中的 this 将引用 $(window),因此您需要创建 this 的副本, 以便能够在这个新函数中引用它。

关于javascript - Javascript 中的变量范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3864097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com