创建JavaScript函数需要技巧

创建JavaScript函数需要技巧,javascript,html,Javascript,Html,我正在编写这段代码来创建一个表,该表一次点亮一个不同的单元格。我已经创建了一个javascript函数来实现这一点,但是所有的单元格都同时点亮。我需要帮助,使一个单元格一次亮起,然后重置为原始颜色 下面是示例代码 <html> <head> <script type="text/javascript"> function change(){ document.getElementById("myTable").

我正在编写这段代码来创建一个表,该表一次点亮一个不同的单元格。我已经创建了一个javascript函数来实现这一点,但是所有的单元格都同时点亮。我需要帮助,使一个单元格一次亮起,然后重置为原始颜色

下面是示例代码

<html>
<head>

    <script type="text/javascript">
        function change(){
            document.getElementById("myTable").rows[1].cells[1].style.backgroundColor ="yellow";
            document.getElementById("myTable").rows[2].cells[2].style.backgroundColor ="green";
            document.getElementById("myTable").rows[2].cells[0].style.backgroundColor ="blue";
            document.getElementById("myTable").rows[0].cells[3].style.backgroundColor ="red";
            document.getElementById("myTable").rows[3].cells[1].style.backgroundColor ="orange";
        }

        function reset () {
            rows[1].cells[1].Value = ""
            rows[1].cells[1].Value = ""
            rows[1].cells[1].Value = ""
            rows[1].cells[1].Value = ""
            i = 0
        }   
    </script>

</head>
<body>
    <h2 style="text-align:center-left"> Sky Apprenticeship Test</h2>
    <table id="myTable"  width="80%" border="1">
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>

        <input id="clickMe" type="button" value="Run Program" onclick="change();" />

        <input type="button" value="Reset" onClick="this.table.reset()" />

    </table>

</body>
</html>

函数更改(){
document.getElementById(“myTable”).rows[1]。cells[1]。style.backgroundColor=“黄色”;
document.getElementById(“myTable”).rows[2]。cells[2]。style.backgroundColor=“绿色”;
document.getElementById(“myTable”).rows[2]。单元格[0]。style.backgroundColor=“蓝色”;
document.getElementById(“myTable”).rows[0]。单元格[3]。style.backgroundColor=“红色”;
document.getElementById(“myTable”).rows[3]。单元格[1]。style.backgroundColor=“橙色”;
}
功能重置(){
行[1]。单元格[1]。值=“”
行[1]。单元格[1]。值=“”
行[1]。单元格[1]。值=“”
行[1]。单元格[1]。值=“”
i=0
}   
天空学徒测验

之后的某个时候,你必须用定时器绘制单元格。如果您在下一次重新绘制之前完成了所有更改,浏览器将重新绘制所有更改。您将希望在人类可见的时间间隔内进行更改。如果浏览器以3毫秒或类似的速度重新绘制,那么每次重新绘制都进行一次更改是没有意义的

var速度=1000,
重置速度=500;
document.getElementById('clickMe')。addEventListener('click',change);
document.getElementById('reset')。addEventListener('click',function(){
重置(document.getElementById('myTable');
});
函数更改(){
承诺,决心
.then(changeColorAndReset.bind(null,'myTable','green',2,2))
.then(changeColorAndReset.bind(null,'myTable','blue',2,0))
.then(changeColorAndReset.bind(null,'myTable','red',0,3))
.then(changeColorAndReset.bind(null,'myTable','orange',3,1))
}
功能复位(表){
Array.prototype.forEach.call(table.rows,function(row){
Array.prototype.forEach.call(row.cells,function(cell){
cell.style.background='';
});
});
}
函数changeColor(表格、颜色、x、y){
返回新承诺(函数(解析){
setTimeout(函数(){
document.getElementById(table).rows[x].cells[y].style.backgroundColor=color;
解决();
},速度);
});
}
函数更改颜色和设置(表格、颜色、x、y){
返回changeColor(表、颜色、x、y){
返回新承诺(函数(解析){
setTimeout(函数(){
document.getElementById(table).rows[x].cells[y].style.backgroundColor='';
解决();
},重置速度);
});
});
}

我的项目

查看Math.random函数,得到两个随机数,行和单元格。然后使用setInterval调用此函数,以便它在您设置的时间间隔上运行。如果您需要更多帮助,请添加注释。您好,谢谢您的帮助,我已经创建了JavaScript,以便它在网页加载时运行,但是我希望函数在下一个函数运行之前重置,即一个单元格亮起,然后重置,然后下一个单元格亮起up@STurk,继续履行诺言。您还可以使用动画库。jQuery或velocity都可以工作。