使用setInterval方法并要求jQuery替代 [英] Usage of setInterval method and asking for jQuery alternate
问题描述
window.setInterval("moveUp(fire,2)","100");
是一个代表div的对象.我想将此对象传递给moveUp方法并连续调用此方法.
in the code above fire is a object represting a div. I want to pass this object to moveUp method and call this method continuosly.
我的问题是window.setInterval不接受像这样的射击对象,并且由于未定义射击而给出错误.虽然我也在其他地方使用火物体.除此之外,我还想知道我是否可以使用jQuery做同样的事情.
My problem is window.setInterval does not accepts fire object like this and gives error as fire is not defined. Although i am using fire object at other places as well. Besides this i also want to know if i can do the same using jQuery.
推荐答案
如果将字符串传递给setInterval
,则它将在全局范围内被eval
ed.不要那样做而是传递一个函数,并保留现有范围.
If you pass a string to setInterval
then it will be eval
ed in the global scope. Don't do that. Pass a function instead, and keep the existing scope.
var fire = something;
var func = function () {
moveUp(fire, 2);
};
setInterval(func,100);
除此之外,我还想知道我是否可以使用jQuery做同样的事情.
Besides this i also want to know if i can do the same using jQuery.
jQuery只是一个JavaScript库.这不是另一种语言.它没有基本JavaScript的替代品.
jQuery is just a JavaScript library. It isn't a different language. It doesn't have alternatives for basic JavaScript.
这篇关于使用setInterval方法并要求jQuery替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!