Skip to content

Instantly share code, notes, and snippets.

var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Adding title afterwards as navigator.canShare just
// takes files as input
shareData.title = "Bits and pieces"
navigator.share(shareData)
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
var filesArray = document.getElementById('share-files').files
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Adding title afterwards as navigator.canShare just
// takes files as input
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Share the data.
} else {
console.log("Your system doesn't support sharing files.");
}
<!DOCTYPE html>
<html>
<body>
<button id="share-button">Share</button>
</body>
<script src='share.js'></script>
</html>
var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
// Check if navigator.share is supported by the browser
if (navigator.share) {
console.log("Congrats! Your browser supports Web Share API")
// navigator.share accepts objects which must have atleast title, text or
// url. Any text or title or text is possible
// Web Share APIs are provided by navigator.share method
if (navigator.share) {
console.log("Web Share APIs are supported")
} else {
console.log("Web Share APIs are not supported")
}
var timerId = setTimeout(function (){
console.log("This function will be removed before it's executed")
}, 100)
clearTimeout(timerId)
// No output will be displayed as the setTimeout callback function
// will be cancelled before it will be executed
function multiplyByTwo (num) {
console.log(num + " multiplied by 2 is " + num*2)
}
// Syntax - setTimeout(callbackMethod, delay, param1, param2, ..)
setTimeout(multiplyByTwo, 100, 4)
// Output:
// 4 multiplied by 2 is 8
function printStatement() {
console.log("I will be printed after 0 milliseconds")
}
setTimeout(printStatement, 0)
console.log("Still, I will be executed first")
/* Output:
Still, I will be executed first
function printStatement() {
console.log("I will be printed after 100 milliseconds")
}
setTimeout(printStatement, 100)
/* ******* Alternate syntax ******************/
/*
setTimeout can be written as this also