gpt4 book ai didi

javascript - 无法为预出价槽复制 JS 变量

转载 作者:行者123 更新时间:2023-12-03 01:02:00 27 4
gpt4 key购买 nike

此代码与在我的网站上转换广告有关。顶部是定义每个广告位的地方。然后我有一个可以加载任何单独广告的函数。此函数需要拉取顶部定义的相应广告位变量,但它似乎不起作用。

var slot1;
googletag.cmd.push(function () {
slot1 = googletag.defineSlot('/50970423/ffn-hb-rect-1', [[300, 250]], 'div-1')
.addService(googletag.pubads());
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
var slot2;
googletag.cmd.push(function () {
slot2 = googletag.defineSlot('/50970423/ffn-hb-rect-ex', [[300, 250]], 'div-2')
.addService(googletag.pubads());
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});

function refreshBid(adUnitName) {
if (adUnitName == '/50970423/ffn-hb-rect-1') {
var slot_to_load = slot1;
}
else if (adUnitName == '/50970423/ffn-hb-rect-ex') {
var slot_to_load = slot2;
}


pbjs.que.push(function () {
pbjs.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: [adUnitName],
bidsBackHandler: function () {
pbjs.setTargetingForGPTAsync([adUnitName]);
googletag.pubads().refresh([slot_to_load]);
}
});
});

}

事情是,如果我将末尾附近的那一行更改为 googletag.pubads().refresh([slot1]);它将完美地工作(当然这仅适用于第一个广告位,因为它是硬编码的,所以它不是问题的解决方案)。有任何想法吗?谢谢!

最佳答案

您正在 if/else 语句内部定义 slot_to_load 变量,因此在底线 slot_to_load未定义。在 if/else 外部定义变量,然后在其中设置其值,您的代码将起作用:

function refreshBid(adUnitName) {
var slot_to_load;
if (adUnitName == '/50970423/ffn-hb-rect-1') {
slot_to_load = slot1;
}
else if (adUnitName == '/50970423/ffn-hb-rect-ex') {
slot_to_load = slot2;
}


pbjs.que.push(function () {
pbjs.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: [adUnitName],
bidsBackHandler: function () {
pbjs.setTargetingForGPTAsync([adUnitName]);
googletag.pubads().refresh([slot_to_load]);
}
});
});

}

关于javascript - 无法为预出价槽复制 JS 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52584089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com