Skip to content

Instantly share code, notes, and snippets.

View rao123dk's full-sized avatar
🎯
Focusing

Dheeraj kumar Rao rao123dk

🎯
Focusing
View GitHub Profile
//@@ I don’t want to listen to your music when I switch tabs
var track = new Audio('./darkhast.mp3');
function isVisible (e){
document.title = document.hidden ? 'please come back and enjoy your music' : 'Enjoying!';
return document.hidden ? track.pause() : track.play();
}
document.addEventListener('visibilitychange',isVisible);
@rao123dk
rao123dk / raoEach.js
Created April 6, 2018 06:26
Own for each for array in JavaScript.
var array_list = [1,2,3,4,5,3];
Array.prototype.raoEach = function(cb){
for (i = 0; i < this.length; i++) {
cb(this[i]);
}
}
array_list.raoEach(function(a){
console.log(a+2);
@rao123dk
rao123dk / allow_multiple_origins.js
Created April 7, 2018 12:36
express application to allow multiple origins
app.use(function(req, res, next) {
var allowedOrigins = ['http://127.0.0.1:8020', 'http://localhost:8020', 'http://127.0.0.1:9000', 'http://localhost:9000'];
var origin = req.headers.origin;
if(allowedOrigins.indexOf(origin) > -1){
res.setHeader('Access-Control-Allow-Origin', origin);
}
//res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:8020');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
<button onClick="start()">start</button>
<button onClick="stop()">stop</button>
<button onClick="pause()">pause</button>
<button onClick="play()">play</button>
<span id="status"></span>
let audioChunks = [];
let mediaRecorder;
//Start the recording
@rao123dk
rao123dk / IME.js
Created May 3, 2018 12:08
Detect user input data when user type with Input method editor(IME).
//The compositionstart, compositionupdate and compositionend events might be helpful. They help you detect when IME input is being used.
EVENTS FIRE
When IME is visible :-
--> compositionstart, compositionupdate, compositionend, input
When IME closed :-
--> keydown, input
$('#id').on('compositionupdate', function(e) {
console.log(e.data);
@rao123dk
rao123dk / typeChecker.js
Created May 23, 2018 07:55
Get the exact type of JavaScript Variables.
function typeChecker(_input){
if([undefined, null, NaN].includes(_input)){
return _input;
}else if(_input === 'true' || _input === 'false'){
return "Boolean";
}else if(new RegExp(".*[a-zA-Z]+.*").test(_input) || _input === ''){
return typeof _input;
}else{
return Number.isInteger(+_input) ? 'integer' : 'flaot';
}
@rao123dk
rao123dk / userProfile.js
Created June 5, 2018 05:31
The code will generate an array of one thousand objects with the properties.
function userProfiler(howMuch){
return new Array(howMuch).fill().map((data,i)=>{
return {
'id' : i,
'name' : 'dummyUser'+i,
'Mobile' : Math.floor(Math.random() * 9000000000) + 1000000000,
'age' : Math.floor(Math.random() * (100-10 + 10) + 1)
}
});
}
@rao123dk
rao123dk / getMax.js
Created June 7, 2018 11:25
Find the highest number in array of objects.
var array = [
{"name":"dheeraj"},
{"name":"download"},
{ "name":"code"},
{"name":"bookbookbookbook"},
{"name":"book"}
];
function getMax() {
return array.reduce((max, p) => p.name.length > max ? p.name.length : max, array[0].name.length);
@rao123dk
rao123dk / multiple.js
Created June 29, 2018 11:30
Multiple events on single element.
['mousemove', 'touchmove'].forEach(function(e) {
window.addEventListener(e, mouseMoveHandler);
});
//Util.js
export function reactBind(data,that){
return data.forEach((fn) => that[fn] = that[fn].bind(that));
}
//myNewComponet.jsx
import reactBind from './Util.js';
class myNewComponet extends React.Component{
constructor(props) {