作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 Javascript,我阅读了有关范围和变量的信息,但无法获得有关如何在函数之间发送变量的信息。请有人解释如何处理这种情况,最好推荐一些东西来阅读。
我想用 30 个不同的参数绘制一张图 30 次,并获取最后一个参数用于检查功能:
function loadImg{
.....
img.onload = function() { // Loading first picture at the start
........
boo.onclick = function() { // the function which by click start all process
var i = 0;
var num; // the variable which I'm going to use for random numbers.
setInterval(function() {
// here I'm generating random numbers
num = Math.floor(Math.random() * imgs.length);
// and then start draw() function, which is going to get the 'num' parameter and draw a pictures with time interval ONLY 30 times
if(i < 30){
draw(num);
i++; }, 2000);
check(num); // after all, I want to run "check()" function which is going to get THE LAST from that 30 generated 'num' parameter and check some information. But is undefined, because it's outside the setInterval function and I don't wont to put it in, because it will starts again and again.
如何获取 check(num) 函数的最后一个参数?
附言抱歉我的英语不好,我一直在尽力描述 a。
最佳答案
您可以在 setInterval()
函数中调用 check(num)
条件:
if(i < 30){
draw(num);
i++;
}
else
{
check(num);
}
然后您还应该结束循环,因为这将无限期地继续运行。
为此,将间隔分配给变量:
var myInterval = setInterval(function() {
然后在调用check()
之前清除间隔:
if(i < 30){
draw(num);
i++;
}
else
{
clearInterval(myInterval);
check(num);
}
关于javascript - 如何将一个函数的值传递给另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30703272/
我是一名优秀的程序员,十分优秀!