14 Replies - 568 Views - Last Post: 23 March 2013 - 01:49 AM
#1
Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 02:39 PM
IS there any way to add a delay to this? If I use something like setTimeout won't that still call the post everytime the user presses a key anyway but just delay it? What can you do about this problem?
Thanks guys
Replies To: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
#2
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 02:45 PM
#4
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:06 PM
What you need is a window of time where that even basically dies and won't fire or if it does fire it won't call post and then it will jquery post again
#5
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:14 PM
Koyume, on 22 March 2013 - 09:45 PM, said:
It may be possible to use setInterval() but I suspect setTimeout() might be easier (or the debounce plug-in that I referenced).
That is, when a key is pressed set a timeout, clearing any existing timeout. In the timeout, submit the request using the currently available characters. But I haven't explored, or tested this, in any detail. I would also check if there are no characters, again cancelling any existing timeout.
The key to getting this to work is to keep cancelling the existing timeout; that is, if it hasn't already been actioned then replace it with the new timeout/delay.
This post has been edited by andrewsw: 22 March 2013 - 03:16 PM
#6
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:31 PM
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script type="text/javascript">
var theTimer;
function updateDiv() {
var theText = document.getElementById("textEntry").value;
if (!theText) {
document.getElementById("divOutput").innerHTML = "Nothing";
} else {
document.getElementById("divOutput").innerHTML = theText;
}
}
function submitChars() {
if (theTimer) {
window.clearTimeout(theTimer);
}
theTimer = window.setTimeout(updateDiv, 1000);
}
</script>
</head>
<body>
<input type="text" id="textEntry" onkeyup="submitChars();">
<div id="divOutput"></div>
</body>
</html>
#7
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:36 PM
Quote
This is folly. Never try to reinvent the wheel if at all possible.
#8
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:37 PM
#9
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 03:52 PM
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script type="text/javascript">
var theTimer;
function updateDiv() {
var theText = document.getElementById("textEntry").value;
var divOutput = document.getElementById("divOutput"),
divErrors = document.getElementById("divErrors");
if (!theText) {
divOutput.innerHTML = "Nothing";
divErrors.innerHTML = "";
} else if (divOutput.previous && divOutput.previous == theText) {
divErrors.innerHTML = "Already submitted..";
} else {
divErrors.innerHTML = "";
divOutput.innerHTML = theText;
divOutput.previous = theText;
}
}
function submitChars() {
if (theTimer) {
window.clearTimeout(theTimer);
}
theTimer = window.setTimeout(updateDiv, 1000);
}
</script>
</head>
<body>
<input type="text" id="textEntry" onkeyup="submitChars();">
<div id="divOutput">Output here</div>
<div id="divErrors">Errors here</div>
</body>
</html>
Sorry, my examples use vanilla JS rather than jQuery - I forgot about jQuery
#10
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 04:15 PM
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var theTimer;
function updateDiv() {
var theText = $("#textEntry").val();
var divOutput = $("#divOutput"),
divErrors = $("#divErrors");
if (!theText) {
divOutput.text("Nothing");
divErrors.text("");
} else if (divOutput.data('previous') && divOutput.data('previous') == theText) {
divErrors.text("Already submitted..");
} else {
divErrors.text("");
divOutput.text(theText);
divOutput.data('previous', theText);
}
}
function submitChars() {
if (theTimer) {
window.clearTimeout(theTimer);
}
theTimer = window.setTimeout(updateDiv, 1000);
}
$(function () {
$("#textEntry").on('keyup', submitChars);
});
</script>
</head>
<body>
<input type="text" id="textEntry">
<div id="divOutput">Output here</div>
<div id="divErrors">Errors here</div>
</body>
</html>
JackOfAllTrades, on 22 March 2013 - 10:36 PM, said:
Quote
This is folly. Never try to reinvent the wheel if at all possible.
I certainly agree with this in principle. In particular, if the jQuery framework is already attached then use it as much as possible. However, if something can be achieved with a few lines of code, then I would prefer not to attach a further plug-in.
#11
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 04:42 PM
#12
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 05:02 PM
adn258, on 22 March 2013 - 11:42 PM, said:
..if you just keep typing keys then, yes, the timeout will keep being cancelled. The timeout won't happen unless the timeout-interval passes (without another key being pressed).
#13
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 22 March 2013 - 05:22 PM
andrewsw, on 22 March 2013 - 05:02 PM, said:
adn258, on 22 March 2013 - 11:42 PM, said:
..if you just keep typing keys then, yes, the timeout will keep being cancelled. The timeout won't happen unless the timeout-interval passes (without another key being pressed).
Right I will check that but that's not what's happening. What's happening is the function keeps getting called thereby submitting the jquery post instead of the timeout getting cancelled as we want.
#14
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 23 March 2013 - 01:30 AM
#15
Re: Keeping Jquery.post From Being Called Over And Over Again DOS Style?
Posted 23 March 2013 - 01:49 AM
adn258, on 23 March 2013 - 08:30 AM, said:
I'm glad you have resolved it, although I am curious why your previous code-version didn't work, as my sample appears to work well. Perhaps you could post some of that code?
|
|

New Topic/Question
Reply


MultiQuote




|