简体   繁体   English

在jQuery中的多个函数之间传递变量?

[英]Passing variables between multiple functions in jQuery?

so i have some code which looks like this : 所以我有一些看起来像这样的代码:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

function checkmyresolutin() {
   // this tests the broswer/window resolution and print to the body [for css purposes]
  var resolution_is = '768px';
  my_important_var_x = resolution_is; // now the var contains the resolution

  return my_important_Var_X;
}

checkmyresolutin(); // for getting the resolutoin on document ready.

$(window).resize(function(){
   checkmyresolutin();  // this is the important part,every time the browser is resized i need to know ,certain function load/unload on certain resolutions.
});

function_abc(); // this is my problem,ill explain bellow,to keep this code block clean.

So after the checkmyresolutin() is called when the browser is resized and it returns the resolution/sceensize and is stored in the my_important_var_x variable ,i need to pass that latest information from that variable to the function_abc(); 因此,在调整浏览器大小并返回分辨率/屏幕大小并存储在my_important_var_x变量中后,调用checkmyresolutin()之后,我需要将最新信息从该变量传递给function_abc(); my_important_var_x function...anyone ? 功能...有人吗?

EDIT : 编辑:

Guys,there are typos but ignore them,i did not copy paste my .js,becasuse it would take up to 400 lines,this is just an example ,the "logic" is what's important here. 伙计们,有错别字,但忽略了它们,我没有复制粘贴我的.js,因为这最多需要400行,这只是一个例子 ,“逻辑”在这里很重要。

I just need somehow to pass content of the enter code here to the function_abc() after the checkmyresolutin() has been run by the jQuery resize function() 我只是需要以某种方式传递的内容enter code herefunction_abc()checkmyresolutin()已被jQuery的运行resize function()

You have a typo: 您有错字:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

should be 应该

var my_important_Var_X = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

See this JSFiddle , with an annoying alert, to show it working. 看到此JSFiddle ,并显示令人讨厌的警报,以显示其工作正常。

I also made a different version where function_abc() is called automatically on resize, just to show how that works. 我还制作了一个不同的版本 ,其中function_abc()在调整大小时自动调用,只是为了说明其工作原理。

Check out this discussion of variable scope in Javascript. 查看有关 Java语言中变量范围的讨论

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

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