RewardedAd.getPreloadedAdCount()
Get the number of ads that have been preloaded and are ready to display.
Syntax
JavaScript
RewardedAd.getPreloadedAdCount()
Description
The getPreloadedAdCount() method returns the exact number of ads that are currently preloaded in memory and ready to be shown instantly. This helps you understand how many ads are in the queue and manage your ad inventory.
ℹ️ About Preloading: BZZE Ads SDK automatically preloads ads in the background when
autoPreload: true is set in init(). By default, it preloads 2 ads for instant playback. Preloaded ads expire after 5 minutes.
Parameters
None
Return Value
Type: number
0- No ads are preloaded (need to wait for preload)1+- Number of ads ready to display- Example:
2means 2 ads are ready in the queue
Examples
Basic Usage
JavaScript
const count = RewardedAd.getPreloadedAdCount();
console.log(`${count} ad(s) ready to play`);
Display Preload Status
JavaScript
function updateAdStatus() {
const count = RewardedAd.getPreloadedAdCount();
const statusElement = document.getElementById('adStatus');
if (count === 0) {
statusElement.textContent = '⏳ Loading ads...';
statusElement.className = 'status loading';
} else if (count === 1) {
statusElement.textContent = '✅ 1 ad ready';
statusElement.className = 'status ready';
} else {
statusElement.textContent = `✅ ${count} ads ready`;
statusElement.className = 'status ready';
}
}
// Update every 2 seconds
setInterval(updateAdStatus, 2000);
updateAdStatus();
Queue Monitor
JavaScript
function monitorAdQueue() {
const count = RewardedAd.getPreloadedAdCount();
const available = RewardedAd.isAdAvailable();
const cooldown = RewardedAd.getCooldownRemaining();
console.log('=== Ad Queue Status ===');
console.log('Preloaded:', count);
console.log('Available:', available);
console.log('Cooldown:', cooldown, 's');
console.log('======================');
if (count === 0) {
console.warn('⚠️ Ad queue is empty! Preloading...');
} else if (count < 2) {
console.log('ℹ️ Low ad inventory');
} else {
console.log('✅ Ad inventory is good');
}
}
monitorAdQueue();
Dynamic Button Badge
JavaScript
function updateButtonBadge() {
const count = RewardedAd.getPreloadedAdCount();
const button = document.getElementById('rewardButton');
const badge = document.getElementById('adBadge');
if (count > 0) {
badge.textContent = count;
badge.style.display = 'block';
button.disabled = false;
} else {
badge.style.display = 'none';
button.disabled = true;
}
}
// HTML:
//
Preload Threshold Alert
JavaScript
const MIN_AD_THRESHOLD = 1;
function checkAdInventory() {
const count = RewardedAd.getPreloadedAdCount();
if (count < MIN_AD_THRESHOLD) {
console.warn('⚠️ Low ad inventory!');
showLowInventoryNotification();
}
}
function showLowInventoryNotification() {
// Show a subtle notification to the user
const notification = document.createElement('div');
notification.className = 'notification';
notification.textContent = 'Loading more ads...';
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 3000);
}
// Check every 5 seconds
setInterval(checkAdInventory, 5000);
React Hook for Preload Count
JavaScript
import { useState, useEffect } from 'react';
function usePreloadedAds() {
const [count, setCount] = useState(0);
const [status, setStatus] = useState('loading');
useEffect(() => {
const interval = setInterval(() => {
const adCount = RewardedAd.getPreloadedAdCount();
setCount(adCount);
if (adCount === 0) {
setStatus('empty');
} else if (adCount < 2) {
setStatus('low');
} else {
setStatus('good');
}
}, 2000);
return () => clearInterval(interval);
}, []);
return { count, status };
}
// Usage in component
function AdButton() {
const { count, status } = usePreloadedAds();
return (
{count} ready
);
}
Advanced Queue Manager
JavaScript
class AdQueueManager {
constructor() {
this.minThreshold = 1;
this.maxThreshold = 3;
this.lastCount = 0;
}
getStatus() {
const count = RewardedAd.getPreloadedAdCount();
const available = RewardedAd.isAdAvailable();
return {
count,
available,
isEmpty: count === 0,
isLow: count < this.minThreshold,
isGood: count >= this.minThreshold && count <= this.maxThreshold,
isFull: count >= this.maxThreshold
};
}
onCountChange(callback) {
setInterval(() => {
const current = RewardedAd.getPreloadedAdCount();
if (current !== this.lastCount) {
callback(current, this.lastCount);
this.lastCount = current;
}
}, 1000);
}
displayUI() {
const status = this.getStatus();
console.log('Queue:', '█'.repeat(status.count) + '░'.repeat(3 - status.count));
}
}
// Usage
const queue = new AdQueueManager();
queue.onCountChange((current, previous) => {
console.log(`Queue changed: ${previous} → ${current}`);
});
queue.displayUI();
How Preloading Works
Automatic Preloading
When you initialize the SDK with autoPreload: true, it automatically:
- Preloads the specified number of ads (default: 2)
- Maintains the queue as ads are shown
- Refreshes expired ads (older than 5 minutes)
- Handles preload failures gracefully
Configuration
JavaScript
RewardedAd.init({
appId: "YOUR_APP_ID",
apiKey: "YOUR_API_KEY",
userId: "user_123",
autoPreload: true, // Enable auto-preload (default: true)
preloadCount: 2, // Number of ads to preload (default: 2)
onAdLoaded: function() {
const count = RewardedAd.getPreloadedAdCount();
console.log(`Ad preloaded! Total: ${count}`);
}
});
Preload Lifecycle
Lifecycle
// Initial state: count = 0
RewardedAd.getPreloadedAdCount(); // 0
// After init with autoPreload: true
// SDK preloads 2 ads in background
// onAdLoaded callback fires twice
RewardedAd.getPreloadedAdCount(); // 2
// After showing one ad
RewardedAd.showAd();
RewardedAd.getPreloadedAdCount(); // 1
// SDK automatically preloads 1 more to maintain queue
// onAdLoaded callback fires once
RewardedAd.getPreloadedAdCount(); // 2
Best Practices
✅ DO:
- Check preload count to show loading states
- Display queue status to users for transparency
- Monitor count to detect preload issues
- Use count to show badge indicators
- Enable
autoPreloadfor best UX
⚠️ DON'T:
- Rely solely on count (always check
isAdAvailable()) - Call too frequently (every 1-2 seconds is enough)
- Manually manage preloading (use
autoPreload) - Assume count > 0 means ad will show (check cooldown too)
Troubleshooting
Count is Always 0
Possible causes:
- ❌
autoPreload: falsein init config - ❌ No internet connection
- ❌ Ad network has no fill
- ❌ Invalid app ID or API key
Count Decreases Without Showing Ads
Possible causes:
- ⏱️ Ads expired (older than 5 minutes)
- 🔄 SDK refreshing stale ads
Related Methods
- isAdAvailable() - Check if ads are ready to show
- showAd() - Display a preloaded ad
- onAdLoaded - Callback when ad is preloaded
- init() - Configure preload settings