sara sara - 11 months ago 68
Javascript Question

How to submit form automatically after x seconds?

I want to submit form automatically after x seconds without pressing the submit button. This sample runs fine, but when I put this sample in my code, it did't work.

What's wrong with my code?

CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.showHint(cm, CodeMirror.hint.anyword);

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
matchBrackets: true,
mode: "text/x-stsrc",
indentUnit: 4,
autoCloseBrackets: true,
highlightSelectionMatches: true,
styleActiveLine: true,
extraKeys: {
"Ctrl-Space": "autocomplete" ,
"Ctrl-Space":function(cm){ CodeMirror.showHint(cm, CodeMirror.hint.anyword); },
"'.'" : function(cmm){ CodeMirror.showHint(cmm, CodeMirror.hint.anyword);},
autohint: true,
readOnly: false,
lineWrapping: true,

editor.on("blur", function(codeMirror) {; });


var orig = CodeMirror.hint.anyword;

CodeMirror.hint.anyword = function(cm) {
var inner = orig(cm) || { from: cm.getCursor(), to: cm.getCursor(), list: []};

return inner;
var timeoutId;
$('form input, form textarea').on('input propertychange change', function() {
console.log('Textarea Change');

timeoutId = setTimeout(function() {
// Runs 1 second (1000 ms) after the last change
}, 1000);

function saveToDB()
console.log('Saving to the db');
form = $('.contact-form');
url: "/echo/json/",
type: "POST",
data: form.serialize(), // serializes the form's elements.
beforeSend: function(xhr) {
// Let them know we are saving
success: function(data) {
var jqObj = jQuery(data); // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
// Now show them we saved and when we did
var d = new Date();
$('.form-status-holder').html('Saved! Last: ' + d.toLocaleTimeString());

// This is just so we don't go anywhere
// and still save if you submit the form
$('.contact-form').submit(function(e) {

CodeMirror.hint.anyword = function(cmm) {
var inner1 = orig(cmm) || { from: cmm.getCursor(), to: cmm.getCursor(), list: []};

return inner1;

so I replace the function function saveToDB() by :


But the submit didn't work after x second

Answer Source

Add Jquery in your fiddle. If you open console in your fiddle. you can see the error message

ceyanebepa.js:45 Uncaught ReferenceError: $ is not defined

Include Jquery and the problem will be solved.