setTimeout和匿名函数 [英] setTimeout and anonymous functions

查看:95
本文介绍了setTimeout和匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我只是在学习Javascript

所提供的一些深度。我不得不说这太棒了。不管怎么说,我是b / b
翻译我的一些旧的经典代码,试图利用

我正在学习的一些新东西,但我'' ma很少坚持

这个(见剪辑):


< snip>

document.getElementById(" button1" ;)。blink = function(duration)

{

var that = this;


var originalDisplay = that.getAttribute( " display'");

var newDipslay;


if(originalDisplay === undefined ||

originalDisplay ==" ;")

{

originalDisplay =" inline";

newDisplay =" none";

}

其他

{

originalDisplay =" none" ;;

newDisplay =" inline" ;;

}


that.setAttribute(" display",newDisplay);

setTimeout(function()

{that.setAttribute(" display",originalDisplay); that = null;},duration);

// setTimeout(function(){alert(''asdf'');},duration);

};


document.getElementById(" button1")。blink(1000);

< / snip>


这不起作用,我不是100%肯定为什么不。 1000毫秒(持续时间)后不会调用匿名

函数。我尝试使用

这个更简单,评论的行,但是也没有执行。


虽然我对Javascript的功能很感兴趣提供,我是

发现很难知道何时/为何/何处使用它们。我确定

会有所改善,因为我认为它可以为我解决问题,但是现在我已经感觉到了'b $ b'感觉相当愚蠢。


任何帮助都将不胜感激。


谢谢,

弗兰克。

解决方案

Frank O''Hara写道:


document.getElementById(" button1")。blink = function(duration)

{

var that = this;


// snipped

};


document.getElementById(" button1")。blink(1000);


>



你认为这个是什么?意味着在这个功能的背景下?你

假设它指的是闪烁函数

属性的元素,但这样的假设是不正确的。


您分配给HTML元素的函数只是被称为

a函数;该元素永远不会以this的形式添加到范围中。你可以通过将元素作为另一个参数传递给

函数来解决这个问题,但这可能会打破你所期待的优雅

to得到。


你可能不得不重新思考这样做的方法。一些

的浏览器会让你修改HTMLElement的原型(意思是你

理论上可以添加一个blink方法,用
$ b $执行b范围内的这个,但其他人(IE)没有(并且这样做可能是一个糟糕的

想法)。


还有其他让你完成类似的东西的方法,但增加的复杂性不是(在我看来)一个值得的

支付。把事情简单化;避免依赖于这个在与DOM交互时,直到你对这个的内容更有经验。真的意思是

不同的背景。


Jeremy


5月29日,11:22 * am,Jeremy< jer ... @ pinacol.comwrote:


Frank O''Hara写道:


* * document.getElementById(" button1")。blink = function(duration)

* * {

* * * * var that = this;


* * * * // snipped

* *};


* * document.getElementById(" button1")。blink(1000);



你认为这个是什么?意味着在这个功能的背景下? *你

假设它指的是闪烁函数

属性的元素,但这样的假设是不正确的。



杰里米,谢谢你的帖子。


起初我假设这个是对实际眨眼

函数的引用,并计划在

函数中添加元素,但事实证明''这个'确实是是对

元素的引用,虽然我承认我不是100%肯定的原因。我想在

上下文中调用函数

(document.getElementById(" button1")。blink(1000);

) ''this''是指向元素的指针。


所以,回到我的原始查询,这是如何使执行

匿名函数setTimeout的?然后,如果我可以让它工作,

然后我可以担心如何实际确保正确的对象

被传递给它。现在该功能没有执行。


谢谢,

Frank


Hello Frank O' '哈拉


我无法分析问题,但我可以指出你在发布的

代码

你有


var newDipslay;



及更高版本


newDisplay =" inline" ;;



大概你打算将这些都称为newDisplay。


--gentsquash


Hello everyone, I''m just learning some of the depth that Javascript
has to offer. I have to say that it is quite amazing. Anyway, I''m
translating some of my older, classical code to try to take advantage
of some of the new things I''m learning but I''m a little stuck with
this one (see snip):

<snip>
document.getElementById("button1").blink = function (duration)
{
var that = this;

var originalDisplay = that.getAttribute("display");
var newDipslay;

if (originalDisplay === undefined ||
originalDisplay == "")
{
originalDisplay = "inline";
newDisplay = "none";
}
else
{
originalDisplay = "none";
newDisplay = "inline";
}

that.setAttribute("display",newDisplay);
setTimeout(function ()
{that.setAttribute("display",originalDisplay);that =null;},duration);
// setTimeout(function () {alert(''asdf'');},duration);
};

document.getElementById("button1").blink(1000);
</snip>

This isn''t working and I''m not 100% certain why not. The anonymous
function is not being called after 1000 msec (duration). I tried with
the easier, commented, line but that didn''t execute either.

While I''m intrigued by the features that Javascript has to offer, I''m
finding it difficult to know when/why/where to use them. I''m sure
that will improve as I see it solve problems for me but for now I''m
feeling rather ''dumb''.

Any help would be appreciated.

Thanks,
Frank.

解决方案

Frank O''Hara wrote:

document.getElementById("button1").blink = function (duration)
{
var that = this;

//snipped
};

document.getElementById("button1").blink(1000);

>

What do you suppose "this" means in the context of this function? You
are assuming that it refers to the element of which the blink function
is a property, but such an assumption is not correct.

The function you assigned to the HTML element is simply being called as
a function; the element is never added to the scope as "this". You
could fix this by passing the element as another parameter to the
function, but this probably breaks the elegance that you were expecting
to get.

You will probably have to re-think the approach for doing this. Some
browsers will let you modify the prototype of HTMLElement (meaning you
could theoretically add a "blink" method that would be executed with
"this" in scope), but others (IE) do not (and doing so is probably a bad
idea).

There are other approaches which would let you accomplish similar
things, but the added complication is not (in my opinion) a worthwhile
payoff. Keep it simple; avoid relying on "this" when interacting with
the DOM until you are more experienced in what "this" really means in
different contexts.

Jeremy


On May 29, 11:22*am, Jeremy <jer...@pinacol.comwrote:

Frank O''Hara wrote:

* * document.getElementById("button1").blink = function (duration)
* * {
* * * * var that = this;

* * * * //snipped
* * };

* * document.getElementById("button1").blink(1000);


What do you suppose "this" means in the context of this function? *You
are assuming that it refers to the element of which the blink function
is a property, but such an assumption is not correct.

Jeremy, thanks for the post.

At first I assumed "this" was a reference to the actual "blink"
function and had planned on adding element as an arguement to the
function but it turns out that ''this'' indeed was a reference to the
element, although admittedly I''m not 100% certain why. I guess in the
context of calling the function
(document.getElementById("button1").blink(1000);
) ''this'' is a pointer to the element.

So, back to my original query which is how to make the execute an
anonymous function in setTimeout? Then if I can get that to work,
then I can worry about HOW to actually ensure that the correct object
is being passed to it. Right now the function is not executing.

Thanks,
Frank


Hello Frank O''Hara

I can''t analyse the problem, but I can point out that in your posted
code
you have

var newDipslay;

and later

newDisplay = "inline";

Presumably you intended these to both refer to "newDisplay".

--gentsquash


这篇关于setTimeout和匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆