2017-01-01 198 views
1

我知道这个问题已被多次询问(是的,我做了一些研究),但我看不到找到适合我需求的解决方案。将指针/参考传递给变量作为参数

我迄今所做的:

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

所以我想,我怎么能改善这个问题,并且我已经想出了一个解决方案,只需要在任何毫秒内执行一次函数。如果该函数已经在{whatever}毫秒中执行过,那么这涉及一个设置为true或false的变量。

我想要完成的任务:

我希望能够设置为,将作为一个标志,以确定是否该功能已经被执行或没有外部变量的引用。

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 
} 

这是如何实现的?

侧面说明

如果这种解释是没有帮助的,你仍然没有得到我想要的:

如何传递给一个变量的引用到一个函数,所以函数可以基于该变量的值来操作?

为什么正常参数是不是一个选项 我要实现某种类型的函数内部递归setTimeout的功能,来检查,如果另一个功能已被执行或者不执行,如果我通过这一个参数,这参数在过程中不能改变。

希望你们能帮助我!

谢谢

谢谢你的所有伟大的答案。你让我学习了很多。我正在采取去抖策略!我标记T.J. Crowder是最好的答案,因为这是一个很好的解释,也是第一个答案。但是再次感谢你们!

+2

http://stackoverflow.com/a/4298672 – Teemu

+0

你可以瀑布的承诺,所以你可以在每个进一步的settimeout调用中使用最后的结果。检查这篇文章https://remysharp.com/2015/12/18/promise-waterfall/#comment-3076688933 – Sebas

回答

2

什么你所描述想要做不会立即说:“使用一个变量的引用”对我来说(如Teemu指出,听起来好像要debouncing)但回答你关于变量引用的问题......

JavaScript没有任何形式的引用变量(不是通过关闭其他,这可能是有问题的在这里)。但是,只要使用一个对象并使用一个属性,就可以轻松地完成所谈论的内容。该房产是“可变”。

简单的例子:

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);

+0

谢谢!这是我将要使用的。 –

0

为什么你不使用setInterval函数,它会做你想要的。

例子:

setInterval(function() { 
    // logic to be implemented 
}, delayInMilliseconds) 
0

这又如何实现呢?

不具有可变。 JS中没有“引用变量”。我可以看到两个简单的解决方案:

  1. 通过一个getter/setter函数:

    function queue(getStatus) { 
        … 
        getStatus() // gets current value 
        … 
    } 
    
    var executed = false; 
    queue(function() { return executed; }); 
    
  2. 通过与属性的对象:

    function queue(status) { 
        … 
        status.executed // gets current value 
        … 
    } 
    
    var status = {executed: false}; 
    queue(status); 
    

我有想出一个解决方案,只需要每个{无论} millisecon执行一次函数DS。如果该函数已经在{whatever}毫秒中执行过,那么这涉及一个设置为true或false的变量。

我看不到为什么这个变量需要是函数的参数,并且可以在外面使用(甚至可以设置?)。只需使用queue内的局部变量即可。

顺便说一句,这个功能被称为反弹,你不必自己写这个。许多实现已经可以在网上找到,有时候也可以作为大型图书馆的一部分。例如参见What does _.debounce do?

2

在JavaScript中的值,如整数,字符串,等等都由值传递。如果你想传递一个引用,你必须传入一个对象到JavaScript函数中。 (JavaScript对象是通过引用传递)

function adjustValues(referenceObject) { 
    referenceObject.foo = 2; 
    referenceObject.bar = "newValue"; 
} 

referenceObject = { 
    foo: 1, 
    bar: "initialValue" 
}; 

adjustValues(referenceObject); 
0

试试下面的例子:

'use strict'; 
 

 
var observable = 0; 
 

 
function incObservable() { 
 
\t ++observable; 
 
\t console.log('incObservable observable: '+observable); 
 
} 
 
function observe() { 
 
\t console.log('observe observable: '+observable); 
 
} 
 

 
var observer = setInterval(observe, 100); 
 

 
setTimeout(function() { 
 
\t incObservable(); 
 
\t setTimeout(function() { 
 
\t \t incObservable(); 
 
\t \t setTimeout(function() { 
 
\t \t \t incObservable(); 
 
\t \t }, 300); 
 
\t }, 300); 
 
}, 300); 
 

 
setTimeout(function() { 
 
\t // Stop obsever 
 
\t clearInterval(observer); 
 
}, 1000); 
 

 
// observe observable: 0 
 
// observe observable: 0 
 
// incObservable observable: 1 
 
// observe observable: 1 
 
// observe observable: 1 
 
// observe observable: 1 
 
// incObservable observable: 2 
 
// observe observable: 2 
 
// observe observable: 2 
 
// observe observable: 2 
 
// incObservable observable: 3 
 
// observe observable: 3