RewardedAd.isAdAvailable()
Check if rewarded ads are available and ready to be displayed.
Syntax
JavaScript
RewardedAd.isAdAvailable()
Description
The isAdAvailable() method checks if there are any preloaded ads ready to be shown to the user. It returns true if at least one ad is available, false otherwise.
This method is useful for:
- Showing/hiding "Watch Ad" buttons based on availability
- Displaying loading states while ads are being preloaded
- Preventing users from clicking when no ads are ready
- Implementing custom UI feedback
Parameters
None
Return Value
Type: boolean
true- At least one ad is preloaded and ready to showfalse- No ads are currently available
Examples
Basic Usage
JavaScript
// Check if ad is available before showing button
if (RewardedAd.isAdAvailable()) {
console.log('✅ Ad is ready to display');
showRewardButton();
} else {
console.log('⏳ No ads available, preloading...');
hideRewardButton();
}
Dynamic Button State
JavaScript
const rewardButton = document.getElementById('rewardButton');
// Update button state every 2 seconds
setInterval(() => {
if (RewardedAd.isAdAvailable()) {
rewardButton.disabled = false;
rewardButton.textContent = '▶️ Watch Ad for Reward';
rewardButton.style.opacity = '1';
} else {
rewardButton.disabled = true;
rewardButton.textContent = '⏳ Loading ads...';
rewardButton.style.opacity = '0.5';
}
}, 2000);
Show Fallback Content
JavaScript
function tryShowAd() {
if (RewardedAd.isAdAvailable()) {
// Ads are ready, show them
RewardedAd.showAd({
rewardPreview: '100 coins'
});
} else {
// No ads available, show alternative
alert('⚠️ No ads available right now. Try again in a moment!');
// Optional: Offer alternative reward method
showAlternativeRewardOptions();
}
}
React Hook Integration
JavaScript
import { useState, useEffect } from 'react';
function GameComponent() {
const [adAvailable, setAdAvailable] = useState(false);
useEffect(() => {
// Check availability every 2 seconds
const interval = setInterval(() => {
const available = RewardedAd.isAdAvailable();
setAdAvailable(available);
}, 2000);
return () => clearInterval(interval);
}, []);
return (
);
}
Best Practices
✅ DO:
- Check availability before calling
showAd() - Update UI in real-time to reflect ad availability
- Show loading states when ads are not ready
- Disable buttons when no ads are available
- Check periodically (every 2-3 seconds)
⚠️ DON'T:
- Call
showAd()without checking availability first - Check too frequently (avoid every frame/ms)
- Rely solely on this without handling
onNoFillcallback - Assume ads will always be available
Related Methods
- isReady() - Check if SDK is initialized
- getPreloadedAdCount() - Get number of preloaded ads
- showAd() - Display a rewarded ad
- onNoFill - Handle no ad availability