如何在没有括号的情况下调用函数时传递参数[Javascript] [英] How to pass parameters when calling a function without the parenthesis [Javascript]

查看:108
本文介绍了如何在没有括号的情况下调用函数时传递参数[Javascript]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为tryMe的函数,我在没有括号的情况下调用它并不是这样,但这个想法就像你在这里做的那样:

I have a function called "tryMe" and I'm calling it without the parenthesis not precisely this but the idea is like what you would do here:

setTimeout(tryMe,200);

如何传递我需要的参数?

how can I pass parameters that I need?

我正在使用一个jquery插件,它允许我调用一个函数,但我必须用括号调用它,或者它在加载时自行执行。

I am using a jquery plugin that enables me to call a function but I have to call it withou the parenthesis or it executes itself upon loading.

推荐答案

setTimeout(function() { tryMe(parm1, parm2); }, 200);

更强大的产品,确保 parm1的值 parm2 在超时发生之前不要更改(@ lincolnk的评论):

A more robust offering, to ensure that the values of parm1, parm2 don't change before the timeout fires (per @lincolnk's comment):

setTimeout(function() {
   var p1 = parm1;
   var p2 = parm2;
   tryMe(p1, p2);
}, 200);

@patrick dw,你是对的,必须先评价。

@patrick dw, you're right, has to be evaluated before.

这篇关于如何在没有括号的情况下调用函数时传递参数[Javascript]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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