Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

3 second timeout then post

Posted on 2013-11-29
6
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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…
Suggested Courses

610 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