Overriding jQuery's get() and post() Functions
I've developed a line of business application for a client where a class of user is automatically logged out after 5 minutes of inactivity. We set it up so every time the user accesses a page (either using AJAX or a 'normal' request) it automatically resets the timer and everything was good. Except for the fact that the user didn't know they were being logged out and then when they submitted a form data would be lost.
To solve this we added a timer to the user's screen to display how much time they had left until they were logged out. The problem was that we couldn't determine how much time was left because every $.post
and $.get
reset the timer on the backend so we couldn't request that information and all those additional requests would add load to the server. We didn't want to have to rewrite all our $.get
and $.post
calls for this one class of user so we had to find a different solution. It turns out you can override them very easily.
$(function(){
// Store a reference to the original remove method.
var originalGetMethod = jQuery.get;
// Define overriding method.
jQuery.get = function(data){
// reset the timer
// Execute the original method.
return originalGetMethod.apply( this, arguments);
}
});
This is a very simple example but you could use this to queue connections to the server or inject a value (like an id for the page/user) into every request.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows