Yhoung Dev
Nov 5 · 2 min read

HOW TO MAKE YOUR MAKE YOUR DEVICE VIBRATE USING JAVASCRIPT

Hello pals Today am gonna show you how to make your device to vibrate using JavaScript …So to do this we need a JS API


DOCUMENTATION OF THE VIBRATION API

It doesn’t have more documentations.. All it has is

“navigator and vibrate”

NAVIGATOR: This is a BOM ( Browser Objects Model)

VIBRATE: This is added to the BOM object to male it vibrate and it takes in parameters too

To use the “vibrate” we need a parentheses () the parentheses holds the duration of for the vibration.. ..it holds the duration in an ordinary (duration ) or in an array manner ([//duration ])


METHOD

The vibration code must be inside a function or else it won’t work.. It only works when an event is been occurred.. ..

Now check the code below 👇

function learn(){

// now write ur code

navigator.vibrate([6000]);

/*this will vibrate for 6seconds */

}

  • Now the vibration duration uses milliseconds to work

U can also create multiple vibration..

MULTIPLE VIBRATION

function sample(){

navigator.vibrate([3000 2000 5000]);

/* now this will vibrate for 3secs wait for 2secs then vibrate for 5secs*/

}

U can test If your browser is compatible with it.. Just use if and else statement

if(window.navigate){

//Run ur code here

}else{

//cant work

}

So after all this now just giveit an EventListener ( best way call it in a function)

😂 😂 😂 copy and pasted this to your editor

Yhoung Dev

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade