【问题标题】:Whatsapp sharing button with a countdown variable带有倒计时变量的 Whatsapp 共享按钮
【发布时间】:2021-09-05 15:51:33
【问题描述】:

网络编码的初学者,我有一个问题来编写一个共享按钮,该按钮会导致 whatsapp 并共享倒计时。 我用静态字符串实现了它,但我无法在其中加载变量......

有人知道我的问题出在哪里吗?

   <script>
// Set the date we're counting down to
var countDownDate = new Date("Aug 20, 2022 12:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// 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);

var countdown_iam = days + "j " + hours + "h "
+ minutes + "m " + seconds + "s "

// Output the result in an element with id="countdown"
document.getElementById("countdown").innerHTML = countdown_iam;

// If the count down is over, write some text 
if (distance < 0) {
    clearInterval(x);
    document.getElementById("countdown").innerHTML = "SURPRISE!!!";
}
}, 1000);

function openWhatsApp() {
    //var countdown_whatsapp = "test";
    //alert(countdown_iam);
    window.open('whatsapp://send?text=' + countdown_iam);
    }

</script>

<h2> WhatsApp sharing Link </h2> 

<!-- create an image icon to open the WhatsApp onclick -->     
<img src="img/whatsapp.png" height="50" size="50" onclick="openWhatsApp()">

感谢您的帮助

【问题讨论】:

    标签: php html countdown


    【解决方案1】:

    countdown_iam 变量是在函数内创建的,并且仅存在于该函数内。所以当其他函数(openWhatsApp)尝试使用该变量时,找不到它,就会出现错误。

    您想从两个不同的函数访问倒计时字符串,因此一种选择是创建一个新函数来生成倒计时字符串并返回它。然后,您可以从两个地方调用该函数。我在这里做了:

    <script>
    
      function getCountdown() {
        var countdownDate = new Date("Aug 20, 2022 12:00:00").getTime();
        var now = new Date().getTime();
        var distance = (countdownDate - now) / 1000;
        if (distance < 0) {
          return 'SURPRISE!!!';
        } else {
          var days = Math.floor(distance / (60 * 60 * 24));
          var hours = Math.floor((distance % (60 * 60 * 24)) / (60 * 60));
          var minutes = Math.floor((distance % (60 * 60)) / 60);
          var seconds = Math.floor(distance % 60);
          return days + "j " + hours + "h " + minutes + "m " + seconds + "s ";
        }
      }
    
      function openWhatsApp() {
        alert('whatsapp://send?text=' + getCountdown());
      }
    
      setInterval(function() {
        document.getElementById("countdown").innerHTML = getCountdown();
      }, 1000);
    
    </script>
    
    <h2>WhatsApp Sharing Link</h2>
    <p id="countdown"></p>
    <p><img src="img/whatsapp.png" height="50" size="50" onclick="openWhatsApp()"></p>

    【讨论】:

    • 过了几个小时才发现它为什么不起作用,而您在几分钟内就解决了我的问题...非常感谢您的回答,非常感谢您的解释。我希望我不会再犯同样的错误。
    • 不客气,阿里曼!很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多