将指针/引用传递给变量作为参数 [英] Passing a pointer/reference to a variable as parameter

查看:91
本文介绍了将指针/引用传递给变量作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已被多次询问(是的,我做了一些研究)但我找不到符合我需要的解决方案。

I know this question has been asked multiple times (yes, I did some research) but I can't see to find a solution that fits my needs.

到目前为止我做了什么:

我正在构建一个功能,跟踪用户向下滚动页面的百分比并显示很好地进入一些进度条。这非常有效,但当我在Chrome上打开开发人员控制台并查看时间轴选项卡(这显示了正在运行的图形中)时,我意识到我的代码非常活跃。它运行了用户在页面上向下滚动的每个像素,这说实话很多。

I was building a function that tracked the percentage of how far the user scrolled down a page and display this nicely into some progressbar. This worked perfectly but when I opened the developer console on Chrome and looked at the Timeline tab (this displays what is being run in a nice graphic), I realised that my code was quite "active". It ran every pixel the user scrolled down the page, which is quite alot to be honest.

所以我想,如何改进这一点我已经提出来了解决方案涉及每{毫米}毫秒执行一次函数。如果函数已经在{whatever}毫秒内执行,则涉及将变量设置为true或false。

So I thought to myself, how can this be improved and I have come up with a solution that involves executing a function only once per {whatever} milliseconds. This involves a variable set to true or false, if the function has already been executed in the {whatever} milliseconds.

我想要完成的任务:

我希望能够设置一个外部变量的引用,该外部变量将作为一个标志来确定该函数是否已被执行。

I want to be able to set a reference to an external variable that will act as a flag to determine if the function has already been executed or not.

function qeue(fn, interval, status){ // this function name might not be very fitting..
    // fn = function to be executed
    // interval = function can only run once between the intervals

    // status = tricky part.. 
    // This should contain a reference to an external variable that is either true or false 
}

如何实现这一目标?

旁注

side note

如果此解释不是'帮助,你仍然得不到我想要的东西:

If this explanation isn't helping, and you still don't get what I want:

如何传递对变量的引用一个函数,以便函数可以根据该变量的值来行动?

为什么普通参数不是一个选项
我想在函数内部实现某种递归的setTimeout功能,检查是否已经执行了另一个函数,如果我将其传递给参数,则该参数在此过程中不会改变。

Why normal parameters are not an option I want to implement some sort of recursive setTimeout functionality inside a function, that checks if another function has been executed or not, if I pass this in to a parameter, this parameter cannot change during the process.

希望你们能帮助我!

谢谢

感谢您的所有好评。你让我学到了很多东西。我正在进行去抖策略!我标记了T.J.克劳德作为最佳答案,因为这是一个很好的解释和第一个。但是再次感谢大家!

Thank you for all your great answers. You made me learn alot. I am going with the debounce strategy! I marked T.J. Crowder as the best answer, because it was a good explanation and one of the first. But thank you all once again!

推荐答案

你所描述的想要做的并不是立刻说使用引用变量对我来说(正如Teemu指出的,听起来像你想要去抖动),但回答你关于引用的问题变量...

What you've described wanting to do doesn't immediately say "use a reference to a variable" to me (as Teemu points out, sounds like you want debouncing), but answering your question about references to variables...

JavaScript没有任何形式的变量引用(除了通过闭包,这可能会有问题)。但是你可以通过使用一个对象并在其上使用一个属性来随时做你正在谈论的事情。该属性是变量。

JavaScript doesn't have any form of references to variables (other than through closures, which might be problematic here). But you can readily do what you're talking about by just using an object and using a property on it. The property is the "variable."

简单示例:

function foo(obj) {
  var counter = 0;
  var timer = setInterval(function() {
    console.log("foo: " + obj.property);
    if (++counter === 5) {
      clearInterval(timer);
    }
  }, 500);
}

var o = {property: "unchanged"};
// Give the "reference" to `property` to `foo`:
foo(o);

// Update it periodically while `foo` is doing its asynchronous thing
setTimeout(function() {
  o.property = "update 1";
}, 1000);
setTimeout(function() {
  o.property = "update 2";
}, 1700);

这篇关于将指针/引用传递给变量作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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