Skip to content

Instantly share code, notes, and snippets.

@aegiz
aegiz / useTimeout.ts
Last active July 27, 2021 06:33
Typescript implementation of useTimeout
/* eslint-disable import/prefer-default-export */
/**
* Create a setTimeout on demand
* See: https://www.joshwcomeau.com/snippets/react-hooks/use-timeout/
*/
import { MutableRefObject, useEffect, useRef } from 'react';
export const useTimeout = (callback: () => void, delay: number): MutableRefObject<number> => {
const timeoutRef = useRef<number>(0); // not sure about this one 🙃
@aegiz
aegiz / ajax request
Created March 31, 2017 13:07
Make AJAX request with Jquery
var callAjax = function(url, type, data, callback) {
$.ajax({
url: url,
type: type,
data: data,
success: function(result) {
if(callback) {
callback(result);
}
}, error: function(MLHttpRequest, textStatus, errorThrown) {
/*
* Check if the different fields are valid
* @param {Object} fields.onMailContainer.warningMsgContainer: container to display warning message
* @param {Object} fields.onMailContainer.mail: mail to check
* @param {Object} fields.onPhoneContainer.warningMsgContainer: container to display warning message
* @param {Object} fields.onPhoneContainer.maphoneil: phone to check
* @param {Object} fields.onCheckContainer.container: container to display warning message
* @param {Object} fields.onCheckContainer.check: checkbox to check (oh oh)
* ...
* @return {Boolean} passedCheck : return true if successfully passed tests. Else return false
@aegiz
aegiz / gist:04cd8f3a756082a479c784b46cfc7af4
Last active April 19, 2016 12:41
Get query params from url
var getQueryParams = function(param){
param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+param+"=([^&#]*)",
regex = new RegExp( regexS ),
results = regex.exec( window.location.href);
return results === null ? false : decodeURIComponent(results[1].replace(/\+/g, " "));
};
@aegiz
aegiz / countAngulatWatchers.js
Last active November 7, 2015 09:36
Count angular watchers (type this in your console)
javascript:(function () { var root = angular.element(document.getElementsByTagName('body')[0]); var watchers = []; var f = function (element) { if (element.data().hasOwnProperty('$scope')) { angular.forEach(element.data().$scope.$$watchers, function (watcher) { watchers.push(watcher); }); } angular.forEach(element.children(), function (childElement) { f(angular.element(childElement)); }); }; f(root); alert(watchers.length);})();