首页 \ 问答 \ 为什么我的循环不起作用?(Why wont my loop work?)

为什么我的循环不起作用?(Why wont my loop work?)

我一直试图循环一个函数,但我似乎无法让它工作,因为我想要它。 下面是我的循环

console.log('START LOOP MY FUNCTION');
a=1;
do{
    console.log('CALL MY FUNCTION');
    a=myFunction(a);
    console.log('EXIT MY FUNCTION');
}while(a==1);
console.log('EXIT LOOP MY FUNCTION');

这是我的功能

function myFunction(b) {
    setTimeout(function(){
        console.log('Start of My function');
        console.log('Inside b is '+b);
        var results = $('#dice > div.game > div > div.bet-history > table > tbody > tr');
        var result = $(results[0]).children('.dice-profit').children().text();
            console.log('BEFORE IF');
            if(result.substring(1) != $(betField).val()){
                console.log('################ERROR FOUND - Result:'+result.substring(1)+' Bet:'+$(betField).val()+' NOT EQUAL');
                console.log('AFTER IF');
                return b=1;
            }else{
                console.log('NO EROR YOU MAY NOW EXIT LOOP');
                console.log('AFTER ELSE');
                return b=0;
            }
        }, 3000);
    }

这是控制台中的输出

new:232 START LOOP MY FUNCTION
new:235 CALL MY FUNCTION
new:237 EXIT MY FUNCTION
new:239 EXIT LOOP MY FUNCTION
new:38 Start of My function
new:39 Inside b is 1
new:42 BEFORE IF
new:48 NO EROR YOU MAY NOW EXIT LOOP
new:49 AFTER ELSE

我认为它应该工作但是从控制台输出的外观来看,它已经在调用myfunction之前退出循环,这意味着即使b = 1它也不会循环。 你能帮助我弄清楚如何循环我的功能吗? 谢谢


I've been trying to loop a function but i cant seem to make it work as i want it to. Below is the my loop

console.log('START LOOP MY FUNCTION');
a=1;
do{
    console.log('CALL MY FUNCTION');
    a=myFunction(a);
    console.log('EXIT MY FUNCTION');
}while(a==1);
console.log('EXIT LOOP MY FUNCTION');

And this is my function

function myFunction(b) {
    setTimeout(function(){
        console.log('Start of My function');
        console.log('Inside b is '+b);
        var results = $('#dice > div.game > div > div.bet-history > table > tbody > tr');
        var result = $(results[0]).children('.dice-profit').children().text();
            console.log('BEFORE IF');
            if(result.substring(1) != $(betField).val()){
                console.log('################ERROR FOUND - Result:'+result.substring(1)+' Bet:'+$(betField).val()+' NOT EQUAL');
                console.log('AFTER IF');
                return b=1;
            }else{
                console.log('NO EROR YOU MAY NOW EXIT LOOP');
                console.log('AFTER ELSE');
                return b=0;
            }
        }, 3000);
    }

This is the output in console

new:232 START LOOP MY FUNCTION
new:235 CALL MY FUNCTION
new:237 EXIT MY FUNCTION
new:239 EXIT LOOP MY FUNCTION
new:38 Start of My function
new:39 Inside b is 1
new:42 BEFORE IF
new:48 NO EROR YOU MAY NOW EXIT LOOP
new:49 AFTER ELSE

I think it should work but from the looks of the output in the console, it already exited the loop before calling myfunction meaning it wont loop even if b=1. Can you guys help me figure out how to loop myfunction? Thanks


原文:https://stackoverflow.com/questions/30745272
更新时间:2022-06-17 17:06

最满意答案

好的,我想我明白你的意思。 这是我制作的小提琴: http//jsfiddle.net/6NLjU/

除了你的代码,我做了两件事。

1)在容器div上添加了一个clearfix,这样即使你浮动内盒也总是有一个高度。

#container_boxes:after {content:""; display:table; clear:both;}

2)while循环结束后,删除最后一个框。 这是因为循环不知道它已超出窗口大小,直到它添加一个不适合的框然后看到容器太大。 所以之后,我删除了一个不适合的盒子。

$("#container_boxes .box:last-child").remove();

OK, I think I understand what you are going for. Here is the working fiddle I made: http://jsfiddle.net/6NLjU/

I did two things in addition to your code.

1) Added a clearfix on the container div, so that it would always have a height even though you are floating the inner boxes.

#container_boxes:after {content:""; display:table; clear:both;}

2) After the while loop finishes, remove the last box. This is because the loop doesn't know that it has exceeded the window size until it adds a box which doesn't fit and then sees that the container is too big. So afterwards, I removed that one box that didn't fit.

$("#container_boxes .box:last-child").remove();

相关问答

更多
  • 您将需要模拟表格布局行为,就像在这个答案中一样 。 这不涉及使用实际的和子元素! 这涉及使用display属性的某些值(即table和table-cell )来模拟表格的布局行为。 您不希望为纯布局使用实际的表格标记,因为这在语义上是不正确的并且不被接受。 如果您的内容是属于表格的实际表格数据,那么使用实际的
    非常有意义。 我会在这里提出非表格的方式,因为这可能是你想要的。 你的div.container元素将需要display: table; 属性使其表现得像
    。 ...
  • 问题 这是由于两个子元素( article[_v-e514def2] )的累积高度推动了父级( article[_v-f4d9afa6] )超过所需级别的高度。 子元素具有margin以及应用于它们的height ,这实际上意味着它们占据父母的总高度为50%+ 50%+边距 。 解析度 要解决此问题,您需要确保总height等于50% 。 有几种方法可以实现这一目标: 使用calc 使用calc我们可以从height移除margin-top : * { box-sizing: border-box; ...
  • 您可以像这样简单地使用flex: form { display: flex; } input[type="text"] { flex: 1 }
  • 好吧,我找到了部分问题的答案,创建了一个函数来调整底部div的大小,并使用setTimeout来轮询更新。 所以现在唯一的问题是,如何将我的顶部div的高度更改为50%而不会导致Chrome阻止它再次小于该尺寸? Ok, I found a much better solution using html5 and draggable: