Solved

3 second timeout then post

Posted on 2013-11-29
6
239 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now