Skip to content

JavaScript Vibration API

Introduction to Vibration API

JavaScript's Vibration API allows web developers to provide haptic feedback to users on compatible devices, such as smartphones and tablets. This guide covers the API's core functionality, practical applications, and best practices for integration.

The Vibration API enables tactile feedback for user actions, contributing to a more immersive and interactive experience in web applications.

Basic Concepts

At its core, the Vibration API is straightforward. It provides a single method, navigator.vibrate(), which accepts either a single number (duration in milliseconds) or an array of numbers (a pattern of vibration and pause durations).


javascript
// Check for browser support before calling
if (navigator.vibrate) {
  // Single vibration for 500 milliseconds
  navigator.vibrate(500);

  // Vibration pattern: vibrate for 200 ms, pause for 100 ms, then vibrate for 400 ms
  navigator.vibrate([200, 100, 400]);
}

Stopping Vibrations To cancel an ongoing vibration, call navigator.vibrate(0) or pass an empty array navigator.vibrate([]). This is useful for resetting feedback states or responding to user cancellation.

Practical Applications

Understanding how to incorporate the Vibration API effectively into web applications can significantly enhance user engagement and satisfaction. Below are examples of practical applications that illustrate the versatility of the Vibration API.

Feedback on User Actions

Providing immediate physical feedback on user actions can reinforce user interactions and improve the intuitiveness of your application.


javascript
document.getElementById('button').addEventListener('click', function() {
  if (navigator.vibrate) {
    navigator.vibrate(100); // Vibrate for 100 milliseconds on button click
  }
});

Notification Alerts

In scenarios where visual or audio alerts may be missed or inappropriate, vibration alerts can serve as an effective alternative.


javascript
// Function to call when a notification is required
function sendNotification() {
  if (navigator.vibrate) {
    navigator.vibrate([500, 200, 500]);
  }
}

Enhancing Gaming Experiences

For web-based games, the Vibration API can be used to add physical feedback during gameplay, enriching the gaming experience.


javascript
// Vibrate at key moments in a game
function gameOver() {
  if (navigator.vibrate) {
    // Long vibration indicates game over
    navigator.vibrate(1000);
  }
}

Best Practices and Considerations

While the Vibration API opens new avenues for user engagement, its implementation should be approached with consideration for the user experience.

  • Respect User Preferences: Always provide an option for users to disable vibration feedback, catering to those who may find it intrusive or have accessibility concerns.
  • Battery Consumption: Be mindful of the potential impact on device battery life, particularly with extensive use of patterns or long vibrations.
  • Test on Multiple Devices: Vibration intensity and pattern perception can vary significantly across devices. Testing on various devices ensures a consistent user experience.
  • Browser Compatibility: The Vibration API is primarily supported on mobile browsers (Chrome, Firefox, Edge). It is not supported in Safari or most desktop browsers. Always verify support before implementation.

Conclusion

JavaScript's Vibration API offers a practical way to engage users through tactile feedback. By integrating the API into web applications, developers can create more immersive and interactive experiences. This guide provides the foundation for using the Vibration API, from basic usage and stopping patterns to practical applications and best practices, helping developers enhance user engagement responsibly.

Practice

Which of the following statements are true regarding the use of JavaScript's Vibration API?

Dual-run preview — compare with live Symfony routes.