Repeat AJAX GET Until a Specific Response is Met in jQuery

Here is how to repeat an AJAX GET call until a specific response is met: function repeated_ajax_check(url) { $.ajax ({ type: ‘GET’, url: url, dataType: ‘json’, success: function(result) { if(result.success) { console.log("We finally met our response!"); console.log(result); //Do something other here, for example: //$(‘.result-message’).html(‘RESPONSE MET!’); //$(‘.result-message’).show(); }else{ console.log("Response not yet met."); console.log(result); wait(1000); repeated_ajax_check(url); } […]

Calculate Elapsed Time of a Function in jQuery

Here is how to calculate elapsed time of a function in jQuery: function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; return h+":"+(m < 10 ? ‘0’+m : m)+":"+(s < 10 ? ‘0’+s : s); }   var seconds = 1;   $(‘#elapsed-time’).html(‘Elapsed time: ‘ + secondsTimeSpanToHMS(seconds)).show();   […]

Convert Seconds to H:M:S in jQuery

Simple function to convert seconds in H:M:S in jQuery: function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; return h+":"+(m < 10 ? ‘0’+m : m)+":"+(s < 10 ? ‘0’+s : s); }function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; […]

Truncate a String by Custom Length in jQuery

Simple function to truncate a string in jQuery: function truncateString(input, max){ if (input.length > max) return input.substring(0, max) + ‘…’; else return input; };function truncateString(input, max){ if (input.length > max) return input.substring(0, max) + ‘…’; else return input; }; Here is an example usage: $(‘#filename’).html("File: " + truncateString(file.name, 50));$(‘#filename’).html("File: " + truncateString(file.name, 50));

Convert POST Fields to Objects in jQuery

Here is a simple function to convert POST fields to object: function postArray(form){ var data = {}; form = $(form).serializeArray(); for(var i in form) data[form[i].name] = form[i].value; return data; }function postArray(form){ var data = {}; form = $(form).serializeArray(); for(var i in form) data[form[i].name] = form[i].value; return data; } You can use it like this: data: […]

How to Sleep/Wait in jQuery (Synchronous)

Here are two sample functions that will wait, causing interruption: function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() – start) > milliseconds){ break; } } }   function wait(ms) { const start = performance.now(); while(performance.now() – start < ms); }function sleep(milliseconds) { […]

How to Install and Configure Mitmproxy on Windows

Download and install Mitmproxy on your Windows machine via: https://mitmproxy.org/#mitmproxy Open and edit Firefox (or other browser) to use the Mitmproxy loopback as proxy: Open Windows Command Prompt (cmd.exe) and run Mitmproxy: cd "C:\Program Files (x86)\mitmproxy\bin" mitmdump.execd "C:\Program Files (x86)\mitmproxy\bin" mitmdump.exe Now you need to install the Mitmproxy CA certificate. Visit http://mitm.it, click on “Other” […]

Exclude Packages from Apt-Get Upgrade on Debian Linux

To exclude/hold a package from apt-get upgrade type: apt-mark hold package_nameapt-mark hold package_name For example, to exclude MariaDB mysql packages type: apt-mark hold libmariadbclient18 mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 mysql-commonapt-mark hold libmariadbclient18 mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 mysql-common Here is an example output: libmariadbclient18 set on hold. mariadb-client-10.3 set on hold. mariadb-client-core-10.3 set […]

What is the Correct JSON Content-Type for HTTP Headers?

Many web services uses different content-type for JSON data, i.e: application/json application/x-javascript text/javascript text/x-javascript text/x-jsonapplication/json application/x-javascript text/javascript text/x-javascript text/x-json But what is the correct content type to use for JSON data? For JSON text you should use application/json (read more on RFC 4627). For JSONP (runnable javascript) with callback, use application/javascript. You should read also […]

What to Select on GRUB-PC Configuration “Install Devices”

Today when I did an “apt-get dist-upgrade” I received this: Looks like GRUB-PC was updated and now it asks: What do you want to do about modified configuration file grub?What do you want to do about modified configuration file grub? I selected “Keep the local version currently installed”. Then it happeared another window asking this: […]