Skip to content

Instantly share code, notes, and snippets.

View abcdeepakr's full-sized avatar
🏋️
kaam karo vyast raho

Deepak Rawat abcdeepakr

🏋️
kaam karo vyast raho
View GitHub Profile
@abcdeepakr
abcdeepakr / calculator.js
Created April 21, 2021 09:06
Import and Export Javascript Functions/Files
// Let us assume that this is the file you are working with and you need some arithmetic functions
//below is the import statement
const math = require("math.js")
let num1 = 10
let num2 = 20
console.log(math.add(num1,num2)) //math.add is how we will be accessing the add functions from the math.js file
make sure that you pass two arguments, since add function requires two arguments.
@abcdeepakr
abcdeepakr / blobToPrint.js
Created January 24, 2022 07:18
Open print dialogue.
const displayPrintDialogue = (generatedBlob) => { // generate blob
var blob = new Blob([generatedBlob], { type: "application/pdf" });
var blobURL = URL.createObjectURL(blob);
let iframe = document.createElement("iframe"); //load content in an iframe to print later
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.src = blobURL;
@abcdeepakr
abcdeepakr / promise.js
Created January 25, 2022 13:17
promises to make async api calls
const fetchData = async () =>{
const ids = [1,2,3,4,5]
let promises = await ids.map(id =>{
await axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`)
.then(res => res.data)
.catch(err => console.log(err)
})
@abcdeepakr
abcdeepakr / asyncAwaitForLoop.js
Created February 10, 2022 07:59
Api calls in a for loop
const axios = require('axios')
const fetch = async (id) =>{
let data = await axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`).then(res => res.data).catch(err => err.message)
return data
}
const main = async () =>{
for(var i=1;i<10;i++){
let data = await fetch(i)
@abcdeepakr
abcdeepakr / delay_loop.js
Last active July 14, 2022 13:46
delay for loop
const timer = ms => new Promise(res => setTimeout(res, ms))
async function load ()
{
for (var i = 0; i < 100; i++) {
// write the logic here, make api calls or anything
await timer(550); // will go into the next one after 550ms
}
}
@abcdeepakr
abcdeepakr / print_div.js
Created October 5, 2022 20:30
Print a div, and load all images before printing
function printDiv() {
var a = window.open('', '', 'height=1000, width=800');
a.document.write('<html>');
response.htmlContent.map(html =>{
// add the html to new window
a.document.write(html);
// always start next content from new page
a.document.write(`<div style = "display:block; clear:both; page-break-after:always;"></div>`);
})
@abcdeepakr
abcdeepakr / eventDelegation
Created January 4, 2023 09:32
Event delegation jq
https://learn.jquery.com/events/event-delegation/
(parentStaticSelector).on(event, dynamicChildSelector, function(){})
@abcdeepakr
abcdeepakr / toc.md
Created January 7, 2023 10:32
Markdown Table of content
@abcdeepakr
abcdeepakr / parseUnix.js
Last active November 8, 2023 09:42
to local from unix
export function parseUnixTimeStamp(unix_timestamp){
var date = new Date(unix_timestamp);
// var hours = date.getHours();
// var minutes = "0" + date.getMinutes();
let parsedDate = date.toLocaleString([], {year: 'numeric', month: 'numeric', day: 'numeric',hour: '2-digit', minute:'2-digit'})
// or
parsedDate = date.toLocaleString([], {year: 'numeric', month: 'short', day: 'short',hour: '2-digit', minute:'2-digit'})
// or
parsedDate = date.toLocaleString([], {year: 'numeric', month: 'long', day: 'long',hour: '2-digit', minute:'2-digit'})