简体   繁体   English

将 API 链接到倒计时按钮,以便在单击计时器开始时显示报价

[英]Link API to a countdown button in order to display quote when timer is clicked to begin

I would like to know, if it's even possible, how to link a random quote generator to a button that also starts a countdown.我想知道,如果可能的话,如何将随机报价生成器链接到一个也开始倒计时的按钮。 I think I am close, but something is missing or Im misusing something.我想我已经很接近了,但是缺少某些东西或我滥用了某些东西。 I spent a good deal of time looking on here for some similar questions, but couldn't quite get it right, so here's my first proper StackOverflow question and here is my JS:我花了很多时间在这里寻找一些类似的问题,但不能完全正确,所以这是我的第一个正确的 StackOverflow 问题,这是我的 JS:

//Variables 
let timerInterval
let min, sec, seconds = (300)

//click start button begin  5 min countdown
   
  //FOR THE FIVE MINUTE TIMER
  timerElFive.addEventListener('click', ()=> {
        if(timerInterval){
          clearInterval(timerInterval)
          return timerInterval = null
        }
        console.log(startTimer())
    })
    //FOR THE QUOTE API ON-CLICK OF FIVE MINUTE TIMER
    timerElFive.addEventListener('click', ()=>{
        fetch("https://api.adviceslip.com/advice")
        .then((response) =>{
            return response.json()
        })
        .then((data)=>{
          let newQuote = {}
          newQuote.quote = data.slip.advice
            document.appendChild(newQuote, data)//quotes.push(newQuote)
            render()
        })
        .catch((err)=>{
            console.log(err)
        })
     })

here is the render function which works to begin and reset counter--Im fairly certain its missing something to display the API:这是渲染 function 用于开始和重置计数器 - 我相当确定它缺少显示 API 的东西:

        //five minute render function
    function render() {
        min = Math.floor(seconds / 60) 
        sec = seconds % 60
        if(sec < 10) {
            timerElFive.innerText =`${min}:0${sec}`
        } else {
            timerElFive.innerText = `${min}:${sec}`
        }

I get an error that says:我收到一条错误消息:

TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
    at main.js:32

Just for context, this is for a simple practice app that generates an inspirational quote when the timer button is clicked and the timer dings when it gets to zero.只是为了上下文,这是一个简单的练习应用程序,它会在单击计时器按钮时生成鼓舞人心的报价,并在计时器归零时发出叮当声。

from what i see ur newQuote is an object, and you are trying to append to the document that object.从我看到你的 newQuote 是一个 object,你正在尝试 append 到 object 的文档。 you already have the response from the endpoint, which is a string.您已经有来自端点的响应,它是一个字符串。 all you need to do is to display that string.您需要做的就是显示该字符串。 i've created an example just for that part, in which i have a paragraph where i display the response.我已经为那部分创建了一个示例,其中我有一个显示响应的段落。

 const timerElFive = document.querySelector('button'); let quoteWrapper = document.querySelector('.quote-wrapper') timerElFive.addEventListener('click', ()=>{ fetch("https://api.adviceslip.com/advice").then((response) =>{ return response.json() }).then((data)=>{ let quote = data.slip.advice; quoteWrapper.innerText = quote; }).catch((err)=>{ console.log(err) }) })
 <button> Click me for quotes </button> <p class="quote-wrapper"> </p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM