同一类型的多个对象的方法中的 setTimeout

发布于 2024-11-28 05:13:21 字数 1406 浏览 0 评论 0原文

我需要在相同类型的对象的方法中使用“setTimeout”的帮助。我使用这段代码来启动我的对象:

    function myObject(param){

        this.content = document.createElement('div');
        this.content.style.opacity = 0;
        this.content.innerHTML = param;

        document.body.appendChild(this.content);

        this.show = function(){
            if(this.content.style.opacity < 1){
                this.content.style.opacity = (parseFloat(this.content.style.opacity) + 0.1).toFixed(1);
                that = this;
                setTimeout(function(){that.show();},100);
            }
        }

        this.hide = function(){
            if(this.content.style.opacity > 0){
                this.content.style.opacity = (parseFloat(this.content.style.opacity) - 0.1).toFixed(1);
                that = this;
                setTimeout(function(){that.hide();},100);
            }
        }
    }

在某处我有 2 个对象:

    obj1 = new myObject('Something here');
    obj2 = new myObject('Something else here');

在 HTML 代码中的某处我使用它们:

    <button onclick="obj1.show()">Something here</button>
    <button onclick="obj2.show()">Something else here</button>

当用户按下一个按钮时,一切都会正常,但如果用户按下一个按钮,并在很短的时间间隔后,他按下另一种是停止第一个按钮触发的动作,只执行第二个按钮的动作。 我知道全局变量“that”成为第二个对象的引用,但我不知道如何创建一个不会阻止先前调用的方法的自动机制。

预先感谢您,如果我犯了一些错误,请原谅我的英语:P

I need help with the use of "setTimeout" in the methods of the objects of the same type. I use this code to initiate my objects:

    function myObject(param){

        this.content = document.createElement('div');
        this.content.style.opacity = 0;
        this.content.innerHTML = param;

        document.body.appendChild(this.content);

        this.show = function(){
            if(this.content.style.opacity < 1){
                this.content.style.opacity = (parseFloat(this.content.style.opacity) + 0.1).toFixed(1);
                that = this;
                setTimeout(function(){that.show();},100);
            }
        }

        this.hide = function(){
            if(this.content.style.opacity > 0){
                this.content.style.opacity = (parseFloat(this.content.style.opacity) - 0.1).toFixed(1);
                that = this;
                setTimeout(function(){that.hide();},100);
            }
        }
    }

Somewhere I have 2 objects:

    obj1 = new myObject('Something here');
    obj2 = new myObject('Something else here');

Somewhere in the HTML code I use them:

    <button onclick="obj1.show()">Something here</button>
    <button onclick="obj2.show()">Something else here</button>

When the user presses one button, everything goes OK, but if the user presses one button and after a short time interval he presses the other one, the action triggered by the first button stops and only the action of the second button is executed.
I understand that the global variable "that" becomes the refence of the second object, but I don't know how to create an automatic mechanism that wouldn't block the previously called methods.

Thank you in advance and sorry for my English if I made some mistakes :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-12-05 05:13:21

如果您需要可取消的内容,请使用 window.setInterval 而不是 setTimeout。 setInterval 返回间隔的句柄,然后可以使用该句柄稍后取消间隔:

var global_intervalHandler = window.setInterval(function() { ... }, millisecondsTotal);

// more code ...

// later, to cancel this guy:

window.clearInterval(global_intervalHandler);

因此,从这里开始,我确信您可以使用您的工程技能和创造力来进行自己的自过期操作 - 如果它们成功执行并完成(甚至不成功)他们取消了自己的间隔。如果另一个进程介入,它可以首先取消该间隔,然后触发其行为。

If you need something cancellable, use window.setInterval instead of setTimeout. setInterval returns a handle to the interval which can then be used to cancel the interval later:

var global_intervalHandler = window.setInterval(function() { ... }, millisecondsTotal);

// more code ...

// later, to cancel this guy:

window.clearInterval(global_intervalHandler);

So from here I'm sure you can use your engineering skills and creativity to make your own self expiring operations - if they execute and complete successfully (or even unsuccessfully) they cancel their own interval. If another process intervenes, it can cancel the interval first and hten fire its behavior.

云柯 2024-12-05 05:13:21

有几种方法可以处理这样的事情,这只是我想到的一种。

首先,我看到您正在编写匿名函数并将其放入 setTimeout 中。我发现将对象的方法绑定到其范围并将其发送到 setTimeout 更优雅。有很多方法可以进行连接,但很快 bind() 将成为标准(您可以自己将其写入自己的支持库以实现浏览器兼容性)。以这种方式执行操作将使您的变量保持在自己的作用域内(全局作用域中没有“那个”变量),并且大大有助于避免此类错误。例如:

function myObject(param){

    // ... snip

    this.show = function(){
        if(this.content.style.opacity < 1){
            this.content.style.opacity = (parseFloat(this.content.style.opacity) + 0.1).toFixed(1);
            setTimeout(this.show.bind(this),100);
        }
    }

    this.hide = function(){
        if(this.content.style.opacity > 0){
            this.content.style.opacity = (parseFloat(this.content.style.opacity) - 0.1).toFixed(1);
            setTimeout(this.hide.bind(this),100);
        }
    }
}

其次,您可能想向对象添加一些动画处理方法。 setTimeout 返回可用于取消计划回调的句柄。如果您实现像 this.registerTimeout() 和 this.cancelTimeout() 这样的东西,可以帮助您确保一次只发生一件事,并将代码的行为与您所描述的疯狂用户点击隔离开来。

There are several ways to handle something like this, here's just one off the top of my head.

First of all, I see you're writing anonymous functions to put inside the setTimeout. I find it more elegant to bind a method of my object to its scope and send that to setTimeout. There's lots of ways to do hitching, but soon bind() will become standard (you can write this into your own support libraries yourself for browser compatibility). Doing things this way would keep your variables in their own scope (no "that" variable in the global scope) and go a long way to avoiding bugs like this. For example:

function myObject(param){

    // ... snip

    this.show = function(){
        if(this.content.style.opacity < 1){
            this.content.style.opacity = (parseFloat(this.content.style.opacity) + 0.1).toFixed(1);
            setTimeout(this.show.bind(this),100);
        }
    }

    this.hide = function(){
        if(this.content.style.opacity > 0){
            this.content.style.opacity = (parseFloat(this.content.style.opacity) - 0.1).toFixed(1);
            setTimeout(this.hide.bind(this),100);
        }
    }
}

Second, you probably want to add some animation-handling methods to your object. setTimeout returns handles you can use to cancel the scheduled callback. If you implement something like this.registerTimeout() and this.cancelTimeout() that can help you make sure only one thing is going on at a time and insulate your code's behavior from frenetic user clicking like what you describe.

时光清浅 2024-12-05 05:13:21

您需要将其作为全局变量吗?只需更改为 var that = this; 您将在函数上下文内使用变量。

Do you need that as global variable ? just change to var that = this; you will use variable inside of the function context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文