创建一个函数startShowingMessage
,它接受两个参数:一个元素和一个作为URL 的字符串.该函数将使用 setInterval
每 1 秒执行一次任务:获取 URL 并将响应文本放入提供的元素的文本内容中.
我创建了函数并且获取工作,但我不知道如何在同一个函数中设置间隔,而不必调用另一个函数.
异步函数 startShowingMessage(elem, url){常量响应 = 等待 fetch(url);常量文本 = 等待响应.文本();elem.textContent = 文本;}
函数部分起作用,因为没有间隔.
您可以根据需要使用 setInterval
函数 startShowingMessage(elem, url){setInterval(异步函数(){常量响应 = 等待 fetch(url);常量文本 = 等待响应.文本();elem.textContent = 文本;}, 1000);}
如果您想了解更多信息这里是 W3Schoolsp>
或者官方文档可以找到根据 3limin4t0r 的建议,这里是 MDN 文档
Create a function startShowingMessage
that takes two parameters: an element and a string that is a URL. The function will use setInterval
to make the following task every 1s: fetch the URL and put the response text into the text content of the provided element.
I made the function and the fetch works, but i don't know how to set interval in the same function, without having to call another function.
async function startShowingMessage(elem, url){
const response = await fetch(url);
const text = await response.text();
elem.textContent = text;
}
The functions works partially because there is no interval.
you can use setInterval
for your need
function startShowingMessage(elem, url){
setInterval(async function(){
const response = await fetch(url);
const text = await response.text();
elem.textContent = text;
}, 1000);
}
if you want to learn more about it here it is on W3Schools
Or the official documentation can be found here on MDN documentation as suggested by 3limin4t0r
这篇关于在函数中使用设置间隔和获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!