Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay(fn, timeout) Higher-Order Function #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Debounce & Throtte JavaScript demo
Debounce, Throttle & Delayed JavaScript demo
=======================================

Visual demo of debounce and throttling in different implementations jQuery Plugin, underscore.js and lodash.js.
Visual demo of debounce, throttling and delaying in different implementations jQuery Plugin, underscore.js and lodash.js.
More in the blog post [Debounce & throttle, a visual explanation](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)

Author David Corbacho Román


![Screenshot](http://drupalmotion.com/sites/default/files/pics/screenshot.png)
![Screenshot](ScreenShot.png)
Binary file added ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,53 @@
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="jquery-throttle-debounce.js"></script>
<script>window.lo = _.noConflict();</script>
<script type="text/javascript">
function delayed(func, timeout)
{
return lo.compose(lo.partialRight(setTimeout, timeout), func.bind.bind(func, func));
}


function sequenced(method, pauseMSecs)
{
var previous = 0;
var timeout = 0;
var invocations = [];
var lastResult;

function callNext()
{
var invocation = invocations.shift();

lastResult = method.apply(invocation.context, invocation.parameters);

if (invocations.length > 0)
{
timeout = setTimeout(callNext, pauseMSecs);
}
else
{
timeout = 0;
}

previous = (new Date()).getTime();
}

return function()
{
invocations.push({context: this, parameters: arguments});

if (timeout == 0)
{
var remaining = pauseMSecs - ((new Date()).getTime() - previous);

timeout = setTimeout(callNext, Math.min(Math.max(0, remaining), pauseMSecs));
}

return lastResult;
};
}
</script>

</head>
<body>
Expand All @@ -108,6 +155,8 @@

<div id="content">
<h2>Mousemove Events:</h2><div id="allEvents"></div>
<h2>Delayed Mousemove Events: delayed(fn, 200);</h2><div id="delayedEvents"></div>
<h2>Sequenced Mousemove Events: sequenced(fn, 200);</h2><div id="sequencedEvents"></div>
<h2>Debounce Inmediate: _.debounce(fn, 200, true);</h2><div id="debounce_true"></div>
<h2>Debounce Inmediate: $.debounce(200, true, fn);</h2> <div id="debouncejtrue"></div>
<h2>Debounce: _.debounce(fn, 200);</h2> <div id="debounce_false"></div>
Expand Down
Loading