An example that contains sliders that show the difference between the throttle and debounce method of underscore
Created
June 22, 2015 16:45
-
-
Save srajagop/b21eec7efe0e505fece5 to your computer and use it in GitHub Desktop.
Example for Throttle and Debounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="normal">I'm a normal element</div> | |
<input type="range" min="0" max="100" data-for="normal" /> | |
<div class="throttled">I'm a throttled element</div> | |
<input type="range" min="0" max="100" data-for="throttled" /> | |
<div class="debounced">I'm a debounced element</div> | |
<input type="range" min="0" max="100" data-for="debounced" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var resizeElements = function resizeElements (event) { | |
var target = $(event.target), | |
relatedTo = target.data('for'); | |
$('.' + relatedTo).css('width', this.value + '%') | |
console.log(relatedTo); | |
} | |
$('[data-for="normal"]').on('input', resizeElements); | |
$('[data-for="throttled"]').on('input', _.throttle(resizeElements, 100)); | |
$('[data-for="debounced"]').on('input', _.debounce(resizeElements, 100)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font-family: 'Montserrat', sans-serif; | |
} | |
div { | |
background: #0092E0; | |
box-sizing: border-box; | |
color: #fff; | |
margin-bottom: 1em; | |
overflow: hidden; | |
padding: 1em; | |
text-overflow: ellipsis; | |
white-space: nowrap; | |
width: 50%; | |
} | |
[type="range"] { | |
margin-bottom: 2em; | |
width: 100%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment