javascript - 横幅刷新代码

标签 javascript

我有横幅代码,它与图像和超链接不同。我所需要的只是 javascript 代码,它将每 30 秒刷新一次此代码,而不刷新页面的其余部分。

此横幅代码将出现在聊天室中。我真的很感激任何帮助。 横幅代码:

<script language='JavaScript' 
        type='text/javascript' 
        src='xxxxxx/banner.php?uname=yyyy&type=2&rows=1' >    
</script>

最佳答案

您应该做的第一件事是将代码包装在函数中。尝试重新调用整个脚本文件导入比仅仅调用函数要困难得多。

一旦所有内容都包装在函数中,您就可以使用 setTimeout 来调用该方法。我会做这样的事情:

function drawBanner() {
    // Do stuff to draw the banner.
    // Make sure you handle the case that the
    // banner is already present in the DOM!
}

function onDrawBannerTimer() {
    // Set this function to fire again after 30 seconds.
    // Note that this will fire it roughly every 30 seconds real-time.
    // You can move this statement to after the drawBanner() call
    // in order to make it 30 seconds between the end of one
    // invocation and the start of the next.
    setTimeout(onDrawBannerTimer, 30 * 1000);
    drawBanner();
}

// Trigger the first invocation when the script is loaded.
onDrawBannerTimer();

关于javascript - 横幅刷新代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4442629/

相关文章:

javascript - JS 匹配字符串中所有出现的子字符串

javascript - 当一个是 bool 值时,为什么 JavaScript 在 == 运算符比较中将原始值转换为数字的基本原理?

javascript - 在 JavaScript 中,如何在新的浏览器窗口中打开页面并滚动到特定位置?

javascript - 固定文本仅在一个 div 内可见

javascript - React props 的变化超出预期

javascript - CORS 错误 header 允许来源 * http 与 https

Javascript/jQuery 插件问题(不更新数据属性)

显示/隐藏 div 的 Javascript 无法正常工作

javascript - jquery压缩错误

javascript - 删除带有热图的 Highchart 顶线