javascript - 连续调用 Javascript 函数

标签 javascript html css function

目标: 我正在尝试使用 setInterval() 创建一个带有基本 Canvas 动画的稍微动态的页面。当用户第一次访问该页面时,会显示背景图像。然后 Canvas 出现在它上面,不透明度逐渐增加到完全不透明的黑色。 Canvas 变黑后,我希望白色文本出现在我的黑色 Canvas 上。

问题: Canvas 过渡和文本同时出现。

我已经研究过这个问题,这里似乎有很多解决方案。我已经在自己的代码中尝试过很多变体。一旦我有两个独立的功能。另一次我尝试调用一个函数,然后连续调用两个函数。我读了一篇关于创建函数数组,然后遍历函数的博客。这个想法很有趣,但我想知道这里是否有更简单的东西就足够了。

我在 jsfiddle 上使用它,但该网站目前不合作。所以这就是我现在所拥有的:

<div id="container">
    <a href="#" id="LftButton"><img src="imageFiles/arrowButtonLft.png" /></a>
    <a href="#" id="RtButton"><img src="imageFiles/arrowButtonRt.png" /></a>
    <div id="canvasWrapper">
        <canvas id="myCanvas" width="675" height="600">
            <p>Your browser doesn't support canvas.</p>
        </canvas>
        <script src="aboutMeScript.js" type="text/javascript"></script>
    </div>
</div>

#myCanvas{
width:675px;
height:600px;
float:left;
}
#canvasWrapper{
width:675px;
height:600px;
margin:auto;
}
#RtButton{
    float:right;
margin-right:34px;
}
#LftButton{
    float:left;
margin-left:34px;
}
#RtButton, #LftButton{
margin-top:200px;
}

var drawing = document.getElementById("myCanvas");
//Initialize drawing context
var ctx = drawing.getContext("2d");
//Canvas commands follow
var alphaCounter = .033;

function fadeCanvas(){
ctx.fillStyle = "rgba(0,0,0," + alphaCounter + ")";
ctx.fillRect(0,0,675,600);
alphaCounter += .03;
}
function display411(){
ctx.fillStyle=('#fff');
ctx.font='bold 14px + "Courier New, Courier, monospace" +';
ctx.fillText('Click the arrow keys forward to reveal the bigger picture.', 100,100);
}
function init(){
setInterval(fadeCanvas,100);
ctx.save();
setTimeout(display411(), 5000)
}
window.onload = init();

想法?

最佳答案

setTimeout(display411(), 5000)
window.onload = init();

当您只想传递它们时,您正在直接执行函数。去掉 ():

setTimeout(display411, 5000);
window.onload = init;

关于javascript - 连续调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221476/

相关文章:

javascript - 根据 <select> 下拉菜单更改 javascript 中的图像

javascript - 控制栏上没有按钮 - mediaelement.js

javascript - Angular 5 Material 设计全 Angular 输入

javascript - 对文件 : protocol 发出 json/jsonp xhr 请求

css - 如何使导航栏永久位于顶部?

javascript - 将 JSON 信息存储在 HTML 文件中,或者反之亦然

html - 溢出自动隐藏位置绝对的内容的表

javascript - Toastr 实现,在 IE 10 中显示错误后

css - 我还需要在 HTML5 中包含类型 ="value"吗?

javascript - 点击 li 应该在其他 div 中打开一个 url 但有一些数据