I have implemented this from oleq: How do I save inline editor contents on the server?
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline( 'editable', {
on: {
instanceReady: function() {
periodicData();
}
}
} );
var periodicData = ( function(){
var data, oldData;
return function() {
if ( ( data = editor.getData() ) !== oldData ) {
oldData = data;
console.log( data );
// Do sth with your data...
}
setTimeout( periodicData, 1000 );
};
})();
The function periodicData will check if the contents changed every second and Do sth. My problem is I will do an ajax post with an expensive database update that cannot afford at the rate of every second. I was thinking that if I can do something along the line of periodicData but check if the editable has not a keyup in the last x seconds instead. That way will save a lot of expensive ajax calls.
I've tried few times but fail to implement the has not a keyup in the last x seconds. The idea is here: How to delay the .keyup() handler until the user stops typing? but I could stitch these two together.
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
// Usage:
$('input').keyup(function() {
delay(function(){
alert('Time elapsed!');
}, 1000 );
});
I appreciate for any hints. Thanks in advance.