使用JavaScript将倒计时计时器作为Google标记管理器中的变量

使用JavaScript将倒计时计时器作为Google标记管理器中的变量,javascript,google-tag-manager,Javascript,Google Tag Manager,我在使用JavaScript在Google标签管理器中实现倒计时功能时遇到了一些问题。我修改了一些在互联网上找到的代码,但不起作用。目标是将变量配置为JavaScript函数,该函数将倒计时到特定的日期和时间。在我在Google中找到的所有代码中都没有return语句,但在GTM中,如果没有这个子句,我就无法保存变量。我该怎么处理呢 function() { 'use strict'; var countDownDate = new Date("May 5, 2021 12:

我在使用JavaScript在Google标签管理器中实现倒计时功能时遇到了一些问题。我修改了一些在互联网上找到的代码,但不起作用。目标是将变量配置为JavaScript函数,该函数将倒计时到特定的日期和时间。在我在Google中找到的所有代码中都没有return语句,但在GTM中,如果没有这个子句,我就无法保存变量。我该怎么处理呢

function() {
  'use strict';

  var countDownDate = new Date("May 5, 2021 12:00:00").getTime();

  var x = setInterval(function() {
  var now = new Date().getTime();
  var distance = countDownDate - now;
    
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
  document.getElementById("demo").innerHTML = days + "D " + hours + "h " + minutes + "m " + seconds + "s ";
    
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
  return;
}
function(){
"严格使用",;
var countDownDate=新日期(“2021年5月5日12:00:00”).getTime();
var x=setInterval(函数(){
var now=new Date().getTime();
var距离=倒计时日期-现在;
变量天数=数学楼层(距离/(1000*60*60*24));
可变小时数=数学楼层((距离%(1000*60*60*24))/(1000*60*60));
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
document.getElementById(“demo”).innerHTML=days+“D”+hours+“h”+minutes+“m”+seconds+“s”;
如果(距离<0){
净间隔(x);
document.getElementById(“demo”).innerHTML=“过期”;
}
}, 1000);
返回;
}
//编辑

我几乎成功了:

function() {


var countDownDate = new Date("April 30, 2021 12:00:00").getTime();

// Get today's date and time
var now = new Date().getTime();
    
  // Find the distance between now and the count down date
var distance = countDownDate - now;
    
  // Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        
  // If the count down is over, write some text 
  //if (distance < 0) {
  //  clearInterval(x);
  //  document.getElementById("demo").innerHTML = "EXPIRED";
    
if (days == '0') {
          return hours + "h " + minutes + "m " + seconds + "s "; }
  else {
          return days + "D " + hours + "h " + minutes + "m " + seconds + "s ";
    }
  }
function(){
var countDownDate=新日期(“2021年4月30日12:00:00”).getTime();
//获取今天的日期和时间
var now=new Date().getTime();
//找出现在和倒计时日期之间的距离
var距离=倒计时日期-现在;
//天、小时、分钟和秒的时间计算
变量天数=数学楼层(距离/(1000*60*60*24));
可变小时数=数学楼层((距离%(1000*60*60*24))/(1000*60*60));
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//如果倒计时结束,写一些文字
//如果(距离<0){
//净间隔(x);
//document.getElementById(“demo”).innerHTML=“过期”;
如果(天数='0'){
返回时数+“h”+分钟+“m”+秒数+“s”}
否则{
返回天数+“D”+小时+“h”+分钟+“m”+秒+“s”;
}
}
但它仍然不能每秒自动刷新。我曾尝试添加setInterval函数,但它不起作用。有人知道吗