# Vibrator > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** > The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import vibrator from '@ohos.vibrator'; ``` ## System Capabilities SystemCapability.Sensors.MiscDevice ## Required Permissions ohos.permission.VIBRATE ## vibrator.vibrate vibrate(duration: number): Promise<void> Triggers vibration with a specific duration. This method uses a promise to return the execution result. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | duration | number | Yes| Vibration duration.| - Return value | Type| Description| | -------- | -------- | | Promise<void> | Promise used to indicate whether the vibration is triggered successfully.| - Example ``` vibrator.vibrate(1000).then(()=>{ console.log("Promise returned to indicate a successful vibration."); }, (error)=>{ console.log("error.code"+error.code+"error.message"+error.message); }); ``` ## vibrator.vibrate vibrate(duration: number, callback?: AsyncCallback<void>): void Triggers vibration with a specific duration. This method uses a callback to return the execution result. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | duration | number | Yes| Vibration duration.| | callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is triggered successfully.| - Example ``` vibrator.vibrate(1000,function(error){ if(error){ console.log("error.code"+error.code+"error.message"+error.message); }else{ console.log("Callback returned to indicate a successful vibration."); } }) ``` ## vibrator.vibrate vibrate(effectId: EffectId): Promise<void> Triggers vibration with a specific effect. This method uses a promise to return the execution result. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | effectId | [EffectId](#effectid) | Yes| String that indicates the vibration effect.| - Return value | Type| Description| | -------- | -------- | | Promise<void> | Promise used to indicate whether the vibration is triggered successfully.| - Example ``` vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{ console.log("Promise returned to indicate a successful vibration."); }, (error)=>{ console.log("error.code"+error.code+"error.message"+error.message); }); ``` ## vibrator.vibrate vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void Triggers vibration with a specific effect. This method uses a callback to return the execution result. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | effectId | [EffectId](#effectid) | Yes| String that indicates the vibration effect.| | callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is triggered successfully.| - Example ``` vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){ if(error){ console.log("error.code"+error.code+"error.message"+error.message); }else{ console.log("Callback returned to indicate a successful vibration."); } }) ``` ## vibrator.stop stop(stopMode: VibratorStopMode): Promise<void> Stops the vibration based on the specified **stopMode**. This method uses a promise to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this method fails to be called. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes| Vibration mode to stop.| - Return value | Type| Description| | -------- | -------- | | Promise<void> | Promise used to indicate whether the vibration is stopped successfully.| - Example ``` vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{ console.log("Promise returned to indicate a successful vibration."); }, (error)=>{ console.log("error.code"+error.code+"error.message"+error.message); }); ``` ## vibrator.stop stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void; Stops the vibration based on the specified **stopMode**. This method uses an asynchronous callback to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this method fails to be called. - Parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes| Vibration mode to stop.| | callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is stopped successfully.| - Example ``` vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){ if(error){ console.log("error.code"+error.code+"error.message"+error.message); }else{ console.log("Callback returned to indicate successful."); } }) ``` ## EffectId Describes the vibration effect. | Name| Default Value| Description| | -------- | -------- | -------- | | EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.| ## VibratorStopMode Describes the vibration mode to stop. | Name| Default Value| Description| | -------- | -------- | -------- | | VIBRATOR_STOP_MODE_TIME | "time" | The vibration to stop is in **duration** mode. This vibration is triggered with the parameter **duration** of the **number** type.| | VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode. This vibration is triggered with the parameter **effectId** of the **EffectId** type.|