简体   繁体   English

Fabric.js文本:已更改事件

[英]Fabric.js text:changed event

I am working with text:changed event its working fine but I need to trigger a function when user stops typing not on every word change for example if user typing and stop typing for 2 seconds then trigger a function. 我正在处理text:changed事件,但工作正常,但我需要在用户停止输入而不是每次更改单词时都触发一个函数,例如,如果用户键入并停止输入2秒钟,然后触发一个函数。

Below is my code to fire event on text change: 以下是我的代码触发文本更改事件的代码:

canvas.on('text:changed', function(e) {
    obj = canvas.getActiveObject();
    newtext = obj.text;
    languageConverterEntoHn(newtext);

    $('#cteditinput').val(newtext);
    $('#cardalltexthex').empty();
    renderalltextfeomcanvas();
});

The function you are looking for is generally called a debounce and you can find it in lodash library or you can write for yourself. 您要寻找的功能通常称为debounce ,您可以在lodash库中找到它,也可以自己编写。

https://lodash.com/ https://lodash.com/

// import lodash

var myFunction = function(e) {
   obj = canvas.getActiveObject();
   newtext = obj.text;
   languageConverterEntoHn(newtext);

   $('#cteditinput').val(newtext);
   $('#cardalltexthex').empty();
   renderalltextfeomcanvas();
}
var myFunctionDebounced = lodash.debounce(myFunction, 2000); // 2 seconds debounce
canvas.on('text:changed', myFunctionDebounced);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM