通常我会在setInterval中引用"this"时指定一个替代的"self"引用.是否有可能在原型方法的上下文中完成类似的事情?以下代码错误.
function Foo() {}
Foo.prototype = {
bar: function () {
this.baz();
},
baz: function () {
this.draw();
requestAnimFrame(this.baz);
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个简单的JavaScript类.
此类的一种方法是使用setInterval函数设置计时器.每次事件触发时我想调用的方法都在同一个类中定义.
问题是,如何将此方法作为参数传递给setInterval函数?
一次尝试是setInterval('this.showLoading(),100).但是不起作用.这个方法访问类属性,所以我需要'this'引用.
这是示例代码:
function LoadingPicture(Id)
{
this.imgArray = null;
this.currentImg = 0;
this.elementId = Id;
this.loadingTimer = null;
}
LoadingPicture.prototype.showLoading = function()
{
if(this.currentImg == imgArray.length)
currentImg = 0;
document.getElementById(this.elementId).src = imgArray[this.currentImg++].src;
}
LoadingPicture.prototype.StartLoading = function()
{
document.getElementById(this.elementId).style.visibility = "visible";
loadingTimer = setInterval("showLoading()", 100);
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个功能:
function a() {
this.b = 1;
this.set = setInterval(function() {console.log(this.b);}, 200);
}
Run Code Online (Sandbox Code Playgroud)
因此,当调用a.set()时,将调用匿名函数.但是当这个函数被触发指向窗口对象时,这不会起作用.使用ab也不是一个好主意,因为可能有多个实例.
什么是这个问题的好方法?
我在一个对象中有一个函数,它设置了一个调用另一个函数的间隔,但是当这个间隔函数被调用时,它会给我一个错误,说 Uncaught TypeError: Object [object Window] has no method
这是我试图理解的代码。
function test2() {
this.timer;
this.say = function(){
console.log("hi");
}
this.start = function() {
//starts the interval function
this.timer = setInterval(this.loop, 1000)
}
this.loop = function() {
//runs every 1 second
this.say(); //gives error -- Uncaught TypeError: Object [object Window] has no method 'say'
}
}
var test = new test2();
test.start();
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
我有这个简单的代码:
var Modules = (function() {
'use strict';
return {
TIMER: function (){
var timer = null;
return {
time: 100,
init: function() {
this.counter();
this.timer = window.setInterval(this.counter, 1000);
},
counter: function() {
this.time -= 1;
if (this.time <= 0) {
window.clearInterval(this.timer);
alert('Time expired');
}
console.log(this.time);
this.viewer();
},
viewer: function() {
document.getElementById('timer').innerHTML = this.time;
}
}
}
};
}());
Modules.TIMER().init();Run Code Online (Sandbox Code Playgroud)
<div id="timer"></div>Run Code Online (Sandbox Code Playgroud)
出问题了,因为我遇到了2个错误:
this.viewer不是函数
和
此时间的NaN
在间隔内运行我的设计模式有什么问题?
将TIMER扩展为复位方法后:
reset: function() {
this.time = 100;
}
Run Code Online (Sandbox Code Playgroud)
并在外部将其称为:
Modules.TIMER().reset();?
我有
这个时间没有定义 …
我有一个带有setInterval的类的简单示例,该类每5秒调用一次main()。谈到print()时,它返回TypeError:this.print不是函数。而且我真的很困。为什么如果我在没有setInterval的情况下调用main(),则它可以正常运行,但是在setInterval的情况下,它将失败?有点奇怪。有没有任何解决方法可在没有此问题的情况下定期调用main()?
"use strict";
class test {
constructor() {
this.interval = setInterval(this.main, 5000);
}
print(){
console.log('Teeeessssttt');
}
main(){
this.print();
}
}
const a = new test();
Run Code Online (Sandbox Code Playgroud) 我有一个必须经常发布事件的 JavaScript 类。代码如下所示:
class TimerService {
constructor(BroadcastService) {
this.Broadcast = BroadcastService;
this.timeout;
this.warningTimer;
this.startTimer();
}
startTimer() {
clearTimeout(this.timeout);
clearTimeout(this.warningTimer);
this.timeout = setTimeout(this.startWarning, 30000);
}
startWarning() {
this.Broadcast.send("timeout-warning");
this.warningTimer = setTimeout(this.logout, 60000);
}
logout() {
this.Broadcast.send("session-expired");
}
}
export default { service: TimerService, name: "TimerService" };
Run Code Online (Sandbox Code Playgroud)
问题是,在 setTimeout 回调函数中,this范围指向window所以我无法访问this.Broadcast. 什么是构建它的最佳方式,以便我可以访问我需要的一切?