javascript,while循环 [英] javascript, while loop

查看:23
本文介绍了javascript,while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的脚本在继续之前等待用户输入(单击按钮),这在其他语言中是可行的,但在 js 中似乎是不可能的.基本上,我希望用户在给定的时间范围内选择一个选项,如果用户选择了错误的选项,他们会被告知..脚本然后继续......否则,如果在一段时间后没有响应...script 只是继续再次向他们播撒正确的答案,但 js 中似乎没有任何内容可以让脚本等待用户输入!我尝试了一个while循环,但这只是js中的一个大不,我使用了settimeout但没有实际效果,因为脚本只是像往常一样继续然后在x时间后执行操作,我尝试设置变量并让脚本仅当它具有特定值时才设置,仅当用户单击时才设置...例如 varprocedure=false,仅当用户单击按钮时才设置为 true,但它仍然不起作用...我尝试了很多其他解决方案,但实际上似乎没有任何效果.我喜欢 while 循环的想法,因为它完全符合我的要求,但是如果完全冻结我的浏览器,是否有更有效的循环类型,它会以与我的浏览器崩溃相同的方式执行?

下面是我的代码,它完全冻结了我的计算机.此方法在 for 循环中调用,循环调用其后的另一个方法.

function getUserResp(){$("#countdown").countdown({seconds: 15});setTimeout("proceed=true", 16000);$("#ans1").click(function(){ansStr=$(this).text();checkAns(ansStr);});$("#ans2").click(function(){ansStr=$(this).text();checkAns(ansStr);});$("#ans3").click(function(){ansStr=$(this).text();checkAns(ansStr);});

想要这样的东西.....或者只是某种循环让脚本在继续之前等待,所以至少它给用户一些时间来响应而不是直接运行!

 做{$(".ans").mouseover(function(){$(this).addClass("hilite").fadeIn(800);});$(".ans").mouseout(function(){$(this).removeClass("hilite");});}同时(继续==假);}

解决方案

你做错了.

浏览器中的 JavaScript 使用事件驱动模型.没有 main 函数,只有在事件发生时调用的回调(例如文档准备好或锚点点击).如果您希望在用户单击某物后发生某事,请为该事物设置监听器.

您所做的只是在每次循环时不断添加一个事件侦听器.

如果你想等待用户输入,那么什么都不做——浏览器等待用户输入(它有一个内部事件循环).您能做的最糟糕的事情是尝试在浏览器的顶部重新实现您自己的事件循环.

您需要学习 JavaScript.尝试像编写另一种语言一样编写 JavaScript 只会导致痛苦和折磨.认真的.

Douglas Crockford 说得最好:

<块引用>

JavaScript 是一种大多数人在使用之前都不会费心学习的语言.你不能用任何其他语言做到这一点,你不应该想要,你也不应该用这种语言做到这一点.编程是一门严肃的事情,你应该对你在做什么有很好的了解,但大多数人认为他们应该能够在没有任何知识的情况下用这种语言编程,而且它仍然有效.这是因为语言具有巨大的表达能力,这并非偶然.

i'm trying to get my script to wait for user input (click of a button) before continuing, this is v feasible in other languages, but seems impossible in js. basically, i want the user to select an option within a given time frame, if the user selects the wrong option, they're told..script then conts...otherwise, if after a certain amount of time theres no response...script just continues again sowing them the correct ans, but there seems to be nothing in js to make the script wait for that user input! ive tried a while loop, but that is just a big no no in js, ive used settimeout but has no real effect because the script just continues like normal then performs an action after x amount of time, ive tried setting variables and letting the script cont only if it is of a particular value, which is set only if the user clicks...eg var proceed=false, this is only set to true if the user clicks a button, but it still doesn't work... ive tried sooo many other solutions but nothing actually seems to be working. i like the idea of a while loop, because it doeas exactly what i want it to so, but if completly freezes my browser, is there a more effecient type of loop that will will peroform in the same manner with crashing my browser?

heres my code below that compltely freezes my computer. this method is called within a for loop which calls another method after it.

function getUserResp(){
    $("#countdown").countdown({seconds: 15});
    setTimeout("proceed=true", 16000);

    $("#ans1").click(function(){
        ansStr=$(this).text();
        checkAns(ansStr);
     });    
    $("#ans2").click(function(){
        ansStr=$(this).text();
        checkAns(ansStr);
    });    
    $("#ans3").click(function(){
        ansStr=$(this).text();
        checkAns(ansStr);
        });

would like something like this.....or just some sort of loop to make the script wait before going ahead so at least it gives the user some time to respond rather than running straight though!

    do{
    $(".ans").mouseover(function(){
        $(this).addClass("hilite").fadeIn(800);
        });  

    $(".ans").mouseout(function(){
        $(this).removeClass("hilite");
        });

    }while(proceed==false);

}

解决方案

You're doing it wrong.

JavaScript in the browser uses an event-driven model. There's no main function, just callbacks that are called when an event happens (such as document ready or anchor clicked). If you want something to happen after a user clicks something, then put a listener on that thing.

What you've done just keeps adding an event listener every time round the loop.

If you want to wait for user input then just don't do anything - the browser waits for user input (it's got an internal event loop). The worst thing you can do is try to reimplement your own event loop on top of the browser's.

You need to learn JavaScript. Trying to write JavaScript like you would another language only leads to pain and suffering. Seriously.

Douglas Crockford said it best:

JavaScript is a language that most people don’t bother to learn before they use. You can’t do that with any other language, and you shouldn’t want to, and you shouldn’t do that with this language either. Programming is a serious business, and you should have good knowledge about what you’re doing, but most people feel that they ought to be able to program in this language without any knowledge at all, and it still works. It’s because the language has enormous expressive power, and that’s not by accident.

这篇关于javascript,while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆