最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ace editor change event and setvalue - Stack Overflow

programmeradmin1浏览0评论

I am listening on ACE editor's change event to handle with user's input while sometimes I will do setvalue() by js.

So is there a way to avoid the setvalue() triggering the change event?

I am listening on ACE editor's change event to handle with user's input while sometimes I will do setvalue() by js.

So is there a way to avoid the setvalue() triggering the change event?

Share Improve this question edited Jul 29, 2015 at 10:14 Johnny Willemsen 3,0021 gold badge16 silver badges17 bronze badges asked Jul 29, 2015 at 9:30 Li_XiaLi_Xia 1111 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

There is no way to avoid change event. But because change event is fired synchronously, you can set a flag to not handle the events created by you. Something like

var fromSetValue = false;
editor.on("change", function() {
    if (!fromSetValue) {
        // user input
    }
})

fromSetValue = true;
editor.setValue("hi")
fromSetValue = false;

You may suppress firing events before setting a new value (and/or doing other manipulations with editor), and restore it after.

const editorValueChangeHandler = () => {
  console.log('Value handled', editor.getValue());
};

editor.off('change', editorValueChangeHandler);
editor.session.setValue('newValue');
// ... other operations with editor...
editor.on('change', editorValueChangeHandler);
发布评论

评论列表(0)

  1. 暂无评论