Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                h1 Throttling and debouncing demo
#app
  .actions
    button.debounce(v-on:click='debounceCall') Debounce
    button.throttle(v-on:click='throttleCall') Throttle
  .info
    h2 {{time}}
    .logs
      .log(v-if='!logs.length', style="text-align: center") There are currently no invocation logs
      .log(v-for="log in logs")
        span.log__time {{log.time}}:
        span.log__type(v-bind:class="{'log__type--debounce': log.type === 'DEBOUNCE'}") {{log.type}}

      
              
            
!

CSS

              
                *
  box-sizing border-box

body
  text-align center
  display flex
  align-items center
  justify-content center
  flex-direction column
  color #fff
  background-color #111
  
  
#app
  display flex
  
.logs
  background-color #fafafa
  border-radius 5px
  color #333
  height 200px
  width  220px
  overflow auto
  text-align left
  margin 10px
  padding 10px

  
.actions
  display flex
  flex-direction column
  align-items center
  justify-content center

  
$debounceColor = #663399
$throttleColor = #19b5fe
button
  &.debounce
    background-color $debounceColor
    &:active
      background-color darken($debounceColor, 10)
  &.throttle
    background-color $throttleColor
    &:active
      background-color darken($throttleColor, 10)
  border none
  color #fff
  padding 6px 12px
  width 130px
  margin 0 0 5px 0
  
.log__type
  margin 0 0 0 4px
  color $throttleColor
  &--debounce
    color $debounceColor
              
            
!

JS

              
                /**
  * debounce function
  * use inDebounce to maintain internal reference of timeout to clear
*/
const debounce = (func, delay) => {
  let inDebounce
  return function() {
    const context = this
    const args = arguments
    clearTimeout(inDebounce)
    inDebounce = setTimeout(() =>
      func.apply(context, args)
    , delay)
  }
}

/**
  * throttle function that catches and triggers last invocation
  * use time to see if there is a last invocation
*/
const throttle = (func, limit) => {
  let lastFunc
  let lastRan
  return function() {
    const context = this
    const args = arguments
    if (!lastRan) {
      func.apply(context, args)
      lastRan = Date.now()
    } else {
      clearTimeout(lastFunc)
      lastFunc = setTimeout(function() {
        if ((Date.now() - lastRan) >= limit) {
          func.apply(context, args)
          lastRan = Date.now()
        }
      }, limit - (Date.now() - lastRan))
    }
  }
}

const app = new Vue({
  el: '#app',
  data: {
    timeFormat: 'h:mm:ss A',
    time: moment().format('h:mm:ss A'),
    logs: [],
  },
  methods: {
    log: function(type) { 
      const newDate = new Date()
      this.logs.unshift({
        type,
        time: moment().format(this.timeFormat)
      })
    },
    debounceCall: debounce(function() { this.log('DEBOUNCE') }, 2000),
    throttleCall: throttle(function() { this.log('THROTTLE') }, 3000),
    updateTime: function() {
      this.time = moment().format(this.timeFormat)
    }
  },
  mounted: function() {
    setInterval(this.updateTime, 1000)
  }
})
              
            
!
999px

Console