cyc115
cyc115

Reputation: 645

CodeMirror change Handler not working (Javascript basics)

I am currently learning javascript and to do so I have started to work on a small code editor project based on the CodeMirror editor. Right now I am trying to implement a hsaEditorChanged():boolean method that returns true when the editor's content was changed from before and no change have happened for the past second. When I run this code my chrome js console gives the below exception :Uncaught TypeError: undefined is not a function at line 71 : e.docLastChanged = new Date.getTime(); . Due to my lack of knowledge in js ,I could not find out what the problem was although it's probably a really dumb one...

Can anyone help me with this ? Thanks so much!

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    //...
});

editor.docLastChanged = new Date().getTime();
editor.hasChanged = true;

editor.hasEditorChanged = function(){
    var t = new Date().getTime();
    if ((t - editor.docLastChanged) > 1000 && editor.hasChanged) { //if no change for 1 sec
        editor.hasChanged = false;
        return true;
    }
    else return false;
}
//register onChange handler to update the editor.docLastChanged variable
var changeHandler = function(e){
    e.docLastChanged = new Date.getTime(); //ERROR HERE.
    e.hasChanged = true;
    console.log("changed");
}

editor.on('change', changeHandler(this));

Upvotes: 0

Views: 486

Answers (1)

Marijn
Marijn

Reputation: 8929

The precedence of new is lower than that of the dot, so you're doing new (Date.getTime)(). Change your code to (new Date).getTime() or simply +new Date to get a millisecond timestamp.

Upvotes: 2

Related Questions