Solved

3 second timeout then post

Posted on 2013-11-29
6
242 Views
Last Modified: 2013-11-29
Hi,

I have a form with a series of checkboxes and dropdown lists. Currently the form is submitted using a button that calls a function 'postData(this)'. However, I would like to change this so that the form no longer requires a button but will submit after 3 seconds of one of the elements being changed.

If checkbox a is checked, the 3 seconds countdown will start, if another checkbox is checked in that time the 3 seconds is reset and starts again.

How would I go about introducing this?

Thanks
Alex
0
Comment
Question by:Al4ddin2
6 Comments
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 500 total points
ID: 39685385
Hi Alex,
this should do the trick:
var timeoutHandle = null;
$(document).ready(function () {
    $('select').on("change", ResetTimeout );
    $('input[type=checkbox]').on("change", ResetTimeout );
});

function ResetTimeout() {
    if (timeoutHandle != null)
    {
        window.clearTimeout(timeoutHandle);
    }
    timeoutHandle = window.setTimeout(SubmitMyForm, 3000);
}
function SubmitMyForm() {
    // Normally you will have here the submit - now we use just an alert
    alert('Form would have been submitted now');
}

Open in new window


Sample:
http://jsfiddle.net/EE_RainerJ/N2xb9/

HTH
Rainer
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39685420
Simple Javascript.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script>
var restart = true;
function timedSubmit() {
	restart = true;	
	var t = setTimeout(function() {
		if (!restart) {		
			document.getElementById('form1').submit();
		}	
		restart = false;
	}, 3000);	
}
</script>
</head>
<body>
<form id="form1">
<input type="checkbox" onChange="timedSubmit()" />
<input type="checkbox" onChange="timedSubmit()" />
<input type="checkbox" onChange="timedSubmit()" />
<input type="checkbox" onChange="timedSubmit()" />
</form>
</body>
</html>

Open in new window

0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39685504
Well I think the coding being presented gives you an answer, I think you realllllllllllly need to think about what you are doing to usability.  Not everyone responds or does things at the same speed; and sometimes users need to go back and correct something.

You are just guaranteeing the the number of incorrect/invalid submissions you get will increase.

What possible reason could you have for wanting to do such a thing? if you explain, maybe we can give you a more user friendly way of doing what you need to do.

Cd&
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Al4ddin2
ID: 39685606
Well essentially this will be used for filtering so they can select a number of checkboxes that will effect the results displayed. It won't really generate 'incorrect submissions' but I understand it may have a load impact on the server.

Thanks, I'll wil give these answers a go.
0
 

Author Comment

by:Al4ddin2
ID: 39685627
Also, is it possible to do something like this?

timeoutHandle = window.setTimeout(SubmitMyForm(param), 3000);
or SubmitMyForm($(this))?

Thanks
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39685661
Hi,
sure. It is just that you have to create your inline function.

function ResetTimeout() {
    if (timeoutHandle != null)
    {
        window.clearTimeout(timeoutHandle);
    }
    timeoutHandle = window.setTimeout(function () {  // code comes here }, 3000);
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question