相关疑难解决方法(0)

等到flag = true

我有这样的javascript函数:

function myFunction(number) {

    var x=number;
    ...
    ... more initializations
    //here need to wait until flag==true
    while(flag==false)
    {}

    ...
    ... do something

}
Run Code Online (Sandbox Code Playgroud)

问题是javascript被困在了一段时间并且卡住了我的程序.所以我的问题是如何才能在函数中间等待,直到flag为true而没有"busy-wait"?

javascript synchronization

68
推荐指数
9
解决办法
15万
查看次数

如何等待ajax请求?

我正在尝试编写一个JS代码,如果数据库中已存在给定的数字,它将取消"btn_submit"按钮.onclick事件.我使用AJAX查询给定数字的数据库,并确定是否应该将数据发送到将上传问题的.php站点.为了确定这一点,我需要numOfRows变量的值,但因为我在AJAX中设置它将保持为0. validation()函数将在我的AJAX查询完成之前完成,这会导致问题始终表明给定的数字不是存在于DB中(numOfRows将始终保持为0).在我将validation()函数的结束行中的numOfRows与0进行比较之前,如何等待AJAX​​查询完成?如果数字中已存在该数字,我需要将false返回到此行:

document.getElementById("btn_submit").onclick = validation;

谢谢!

var textAreaList;
var numOfRows = 0;
var finished = false;

document.getElementById("btn_submit").onclick = validation;

textAreaList = document.getElementsByClassName("text_input");

function validation() {
    loadNumRows();

    try {
        document.getElementById('failure').hidden = true;
    }
     catch(e) {
         console.log(e.message);
     }
    textAreaList = document.getElementsByClassName("text_input");
    var failValidation = false;
    for (var i = 0; i < textAreaList.length; i++) {
        console.log(textAreaList[i]);
        if (textAreaList[i].value == "") {
            textAreaList[i].style.border = "2px solid #ff0000";
            failValidation = true;
        } else {
            textAreaList[i].style.border = "2px solid #286C2B";
        }
    }

    return !(failValidation || …
Run Code Online (Sandbox Code Playgroud)

javascript php ajax jquery

13
推荐指数
4
解决办法
3万
查看次数

标签 统计

javascript ×2

ajax ×1

jquery ×1

php ×1

synchronization ×1