在JS中加载Facebook广告会减慢游戏速度

时间:2018-08-18 06:03:41

标签: javascript html5 facebook-apps facebook-ads-api

我实现了Facebook Interstitial,并且在我的JS游戏中添加了RewardedVideo。我首先将广告加载到 document.ready

$(document).ready(function() {

//Some game logic

FBInstant.initializeAsync().then(function() {
    FBInstant.startGameAsync().then(function() {
        var supportedAPIs = FBInstant.getSupportedAPIs();
        if (supportedAPIs.includes('getInterstitialAdAsync') && supportedAPIs.includes('getRewardedVideoAsync')) {
            //
            // Preload Interstitial
            //
            FBInstant.getInterstitialAdAsync(
                INTERSTITIAL_PLACEMENT_ID // Your Ad Placement Id
            )
                .then(function(interstitial) {
                    // Load the Ad asynchronously
                    preloadedInterstitial = interstitial;
                    return preloadedInterstitial.loadAsync();
                })
                .then(function() {})
                .catch(function(err) {
                    displayError('Interstitial failed to preload: ' + err.message);
                });
            //
            // Preload Rewarded
            //
            FBInstant.getRewardedVideoAsync(
                REWARDED_PLACEMENT_ID // Your Ad Placement Id
            )
                .then(function(rewarded) {
                    // Load the Ad asynchronously
                    preloadedRewardedVideo = rewarded;
                    return preloadedRewardedVideo.loadAsync();
                })
                .then(function() {})
                .catch(function(err) {
                    displayError('Rewarded video failed to preload:' + err.message);
                });
        } else {
            displayError('Ads not supported in this session');
        }
    });
});
});

当我需要展示广告时,请调用以下方法

function showInterstitial() {
preloadedInterstitial
    .showAsync()
    .then(function() {
        // Perform post-ad success operation
        console.warn('Interstitial ad finished successfully');
    })
    .catch(function(e) {
        console.error(e.message);
    });
preloadedInterstitial.loadAsync(); <-- not sure
}

如您所见,每次需要新广告时,我都可以 preloadedInterstitial.loadAsync(); ,但我认为这会导致游戏运行缓慢。

与奖励视频相同

function showRewardedVideo() {
preloadedRewardedVideo
    .showAsync()
    .then(function() {
        life++;
        showSplash();
    })
    .catch(function(e) {
        displayError(e.message);
    });
    preloadedRewardedVideo.loadAsync();
}

有什么建议吗?

0 个答案:

没有答案