相关疑难解决方法(0)

在对象原型方法中引用setInterval/setTimeout中的"this"

通常我会在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 scope lexical-scope

32
推荐指数
1
解决办法
3万
查看次数

通过setInterval事件调用类原型方法

我有一个简单的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)

javascript

27
推荐指数
3
解决办法
2万
查看次数

如何将"this"传递给window setInterval

假设我有一个功能:

function a() {
    this.b = 1;
    this.set = setInterval(function() {console.log(this.b);}, 200);
}
Run Code Online (Sandbox Code Playgroud)

因此,当调用a.set()时,将调用匿名函数.但是当这个函数被触发指向窗口对象时,这不会起作用.使用ab也不是一个好主意,因为可能有多个实例.

什么是这个问题的好方法?

javascript

8
推荐指数
3
解决办法
1万
查看次数

无法在javascript中使用this.function_name获取setInterval函数来调用另一个函数

我在一个对象中有一个函数,它设置了一个调用另一个函数的间隔,但是当这个间隔函数被调用时,它会给我一个错误,说 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)

感谢您的帮助!

javascript

3
推荐指数
1
解决办法
2582
查看次数

this.method在setInterval中不起作用

我有这个简单的代码:

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();

我有

这个时间没有定义 …

javascript setinterval

3
推荐指数
1
解决办法
70
查看次数

setInterval中的JS类方法不起作用

我有一个带有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 setinterval

2
推荐指数
1
解决办法
207
查看次数

在 setTimeout 函数中访问类变量

我有一个必须经常发布事件的 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. 什么是构建它的最佳方式,以便我可以访问我需要的一切?

javascript angular

0
推荐指数
1
解决办法
1282
查看次数

标签 统计

javascript ×7

setinterval ×2

angular ×1

lexical-scope ×1

scope ×1