Solved

3 second timeout then post

Posted on 2013-11-29
6
244 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

695 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