[jquery-dev] Re: suggestion: delayed related AJAX requests
I agree that this is a common use case, and also that it would be better implemented as an optional parameter to ajax rather than a wholly separate method. I find I also use similar code within animations, so perhaps the code could be implemented in such a way as to also be exposed as a utility function. Perhaps: $.delay(function, delay) - returns a function handler which can be called many times, but the registered function will only fire once, [delay] milliseconds after the last time it was called. The innards of the function would be mostly as Miloš has above. A potentially useful corollary might be: $.throttle(function, delay) - returns a function handler which could be called as many times as desired, but the registered function would only fire every [delay] millis at most. Names are just off the top of my head, of course. oliver On Mar 27, 3:27 pm, Daniel Friesen nadir.seen.f...@gmail.com wrote: Rather than an entire other function, what about just a {delay: ms} to .ajax ~Daniel Friesen (Dantman, Nadir-Seen-Fire) Miloš Rašić wrote: Here's a suggestion for a feature that I think would be useful to AJAX developers. Sometimes, we need an AJAX feature that could allow a user to cause a large number of consequent AJAX requests, for example in auto-complete for searches (like Google Suggest) or filter forms that submit whenever something is changed in them. In a case like this, especially when the user has a slow internet connection, we have no way of knowing if the AJAX responses will arrive in the same order as the requests were sent. A solution to this is to wait for some time to make sure that the user has stopped manipulating the controls that cause AJAX requests before sending a request. At the moment, I do it like this: function submit_filter() { $('#item_list').html('loading...'); $.post('some url',$('#filter').serialize(),function(data) { $('#item_list').html(data); }); return false; } $('.ajax_control').change(function() { clearTimeout(ajaxTimeout); ajaxTimeout = setTimeout(submit_filter,2000); }); Basically, whenever a control is changed a timeout for ajax request is reset to 2 seconds. A request is sent only when a user changes something an hasn't made new changes last 2 seconds. Since this is a frequent use case, it would be great if there would be a $.post_delayed() function which would have additional 2 parameters: an id, which would be used to identify related controls, and a time interval to wait before submitting the request. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups jQuery Development group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~--~~~~--~~--~--~---
[jquery-dev] Re: suggestion: delayed related AJAX requests
Quick passes: jQuery.delay = function(callback, delay) { var timeout; return function() { clearTimeout(timeout); timeout = setTimeout(callback, delay); } } jQuery.throttle = function(callback, delay) { var prev = new Date().getTime() - delay; return function() { var now = new Date().getTime(); if (now - prev delay) { prev = now; callback.call(); } } } On Mar 27, 11:38 pm, oliver oliverlan...@gmail.com wrote: I agree that this is a common use case, and also that it would be better implemented as an optional parameter to ajax rather than a wholly separate method. I find I also use similar code within animations, so perhaps the code could be implemented in such a way as to also be exposed as a utility function. Perhaps: $.delay(function, delay) - returns a function handler which can be called many times, but the registered function will only fire once, [delay] milliseconds after the last time it was called. The innards of the function would be mostly as Miloš has above. A potentially useful corollary might be: $.throttle(function, delay) - returns a function handler which could be called as many times as desired, but the registered function would only fire every [delay] millis at most. Names are just off the top of my head, of course. oliver On Mar 27, 3:27 pm, Daniel Friesen nadir.seen.f...@gmail.com wrote: Rather than an entire other function, what about just a {delay: ms} to .ajax ~Daniel Friesen (Dantman, Nadir-Seen-Fire) Miloš Rašić wrote: Here's a suggestion for a feature that I think would be useful to AJAX developers. Sometimes, we need an AJAX feature that could allow a user to cause a large number of consequent AJAX requests, for example in auto-complete for searches (like Google Suggest) or filter forms that submit whenever something is changed in them. In a case like this, especially when the user has a slow internet connection, we have no way of knowing if the AJAX responses will arrive in the same order as the requests were sent. A solution to this is to wait for some time to make sure that the user has stopped manipulating the controls that cause AJAX requests before sending a request. At the moment, I do it like this: function submit_filter() { $('#item_list').html('loading...'); $.post('some url',$('#filter').serialize(),function(data) { $('#item_list').html(data); }); return false; } $('.ajax_control').change(function() { clearTimeout(ajaxTimeout); ajaxTimeout = setTimeout(submit_filter,2000); }); Basically, whenever a control is changed a timeout for ajax request is reset to 2 seconds. A request is sent only when a user changes something an hasn't made new changes last 2 seconds. Since this is a frequent use case, it would be great if there would be a $.post_delayed() function which would have additional 2 parameters: an id, which would be used to identify related controls, and a time interval to wait before submitting the request. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups jQuery Development group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~--~~~~--~~--~--~---
[jquery-dev] Re: suggestion: delayed related AJAX requests
Rather than an entire other function, what about just a {delay: ms} to .ajax ~Daniel Friesen (Dantman, Nadir-Seen-Fire) Miloš Rašić wrote: Here's a suggestion for a feature that I think would be useful to AJAX developers. Sometimes, we need an AJAX feature that could allow a user to cause a large number of consequent AJAX requests, for example in auto-complete for searches (like Google Suggest) or filter forms that submit whenever something is changed in them. In a case like this, especially when the user has a slow internet connection, we have no way of knowing if the AJAX responses will arrive in the same order as the requests were sent. A solution to this is to wait for some time to make sure that the user has stopped manipulating the controls that cause AJAX requests before sending a request. At the moment, I do it like this: function submit_filter() { $('#item_list').html('loading...'); $.post('some url',$('#filter').serialize(),function(data) { $('#item_list').html(data); }); return false; } $('.ajax_control').change(function() { clearTimeout(ajaxTimeout); ajaxTimeout = setTimeout(submit_filter,2000); }); Basically, whenever a control is changed a timeout for ajax request is reset to 2 seconds. A request is sent only when a user changes something an hasn't made new changes last 2 seconds. Since this is a frequent use case, it would be great if there would be a $.post_delayed() function which would have additional 2 parameters: an id, which would be used to identify related controls, and a time interval to wait before submitting the request. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups jQuery Development group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~--~~~~--~~--~--~---