简体   繁体   English

将变量传递给另一个函数

[英]pass variable to another function

I create a variable in one function, and then call another function which I need to pass this variable to, how do I do this? 我在一个函数中创建了一个变量,然后调用了我需要将该变量传递给的另一个函数,我该怎么做?

code at the moment: 目前的代码:

$('.search-btn').click(function(){
var desiredPage = $(this).attr('goto');
//other code is here
setTimeout(showDesiredPage(), 600);
});

then: 然后:

function showDesiredPage(desiredPage){
// other code happens
$('#'+desiredPage).addClass('current-page');
}

thanks! 谢谢!

setTimeout(showDesiredPage(), 600);

is not correct, it needs to be 是不正确的,它必须是

setTimeout(showDesiredPage, 600);

Please notice the missing brackets after the function! 请注意功能后缺少的括号! The first line hands over the return value of your (immediately executed) function to the setTimeout, while the latter hands over the function reference which then gets executed after 600ms! 第一行将您(立即执行的)函数返回值移交给setTimeout,而后一行将函数引用移交给setTimeout! So in the first example your code fires immediately and the setTimeout then tries to invoke the return value of your function. 因此,在第一个示例中,您的代码立即触发,然后setTimeout尝试调用函数的返回值。

This is the reason, why you can't hand over parameters in the "obvious" way. 这就是为什么您不能以“显而易见的”方式移交参数的原因。 If you want to do this, you need an (anonymous) function wrapper 如果要执行此操作,则需要一个(匿名)函数包装器

setTimeout(function(){showDesiredPage(desiredPage)}, 600);

This passes a function object (=kind of the same like a function reference) to setTimeout which then will get invoked after 600ms. 这会将一个函数对象(=类似函数引用的同类)传递给setTimeout,然后将在600ms之后调用它。 Now, inside the function you can call your desired function with parameters in the same way you would normally do. 现在,您可以在函数内部以与通常相同的方式使用参数调用所需的函数。

There is a possibility to hand over parameters directly (see MDN:setTimeout) but unfortunately this syntax is not supported by Internet Exploder. 可以直接移交参数(请参见MDN:setTimeout),但是遗憾的是Internet Exploder不支持此语法。

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);

Try this: 尝试这个:

$('.search-btn').click(function () {
    var desiredPage = $(this).attr('goto');
    //other code is here
    setTimeout(function () {
        showDesiredPage(desiredPage);
    }, 600);
});

This will pass the variable desiredPage to the function showDesiredPage() . 这会将变量desiredPage传递给函数showDesiredPage()

You need to include the variable that you want to pass to the function(). 您需要包括要传递给function()的变量。

$('.search-btn').click(function(){ var desiredPage = $(this).attr('goto'); //other code is here setTimeout(showDesiredPage(desiredPage), 600); }); $('。search-btn')。click(function(){var wantedPage = $(this).attr('goto'); //其他代码在这里setTimeout(showDesiredPage(desiredPage),600);});

 
 
 
 
  
  
  function showDesiredPage(desiredPage){ // other code happens $('#'+desiredPage).addClass('current-page'); }
 
 
  

EDIT: The above will pass the value, but the the delay in time will not work. 编辑:以上将通过值,但时间延迟将不起作用。

It should be like 应该像

 $('.search-btn').click(function(){ var desiredPage = $(this).attr('goto'); //other code is here setTimeout(function () { showDesiredPage(desiredPage); }, 600); }); function showDesiredPage(desiredPage){ // other code happens $('#'+desiredPage).addClass('current-page'); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM