Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function() {
- // cancel will either:
- // 1. do nothing (was idle)
- // 2. clear debounce timer (waiting for user to stop typing)
- // 3. cancel POST to interpret (debounce timer triggered, i.e. kicked off POST after user stopped typing but user started typing again)
- var cancel = function() {};
- $(document).on('change keyup paste', 'textarea', function() {
- // cancel whatever's going on whenever user does something
- cancel();
- var string = $(this).val();
- var $this = $(this);
- var firki_preview;
- if (string.indexOf("'[") >= 0) {
- // start debounce timer to wait for user to stop typing
- var timeoutId = setTimeout(function() {
- var post = $.post('/api/v1/firki/interpret',
- { firki_string: string },
- function(data) {
- // interpret finished uninterrupted, put cancel back to do nothing (idle)
- cancel = function() {};
- var preview_box = $('div.firki_preview');
- // special case for multiple posts edit
- if (preview_box.length > 1) {
- preview_box = $('div.firki_preview', $this.parents('.post'));
- }
- if (preview_box.length) {
- preview_box.html(data[0]);
- }
- else {
- var preview_box = $('<div class="firki_preview inner">' + data + '</div><div class="hMiniSeperator"></div>');
- $this.after(preview_box);
- }
- }
- );
- // POST kicked off, make cancel abort the POST if user starts typing again before POST is finished
- cancel = function() {
- post.abort();
- };
- }, 150); // <<< CHANGE 150 HERE TO YOUR LIKING
- // make cancel abort the debounce timer if user is still typing before timer fires
- cancel = function() {
- clearTimeout(timeoutId);
- };
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment