完成之前的工作后立即调用功能

时间:2018-01-24 13:08:50

标签: javascript

我在onclick中使用此功能:

function showUser(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
        }
    };
    xmlhttp.open("GET","reseller.php?ostan="+str,true);
    xmlhttp.send();
    get_fb()
}
}

我需要在完成之前的工作后立即调用get_fb()函数,但get_fb()会在之前的作业完成之前自行启动它。

当我设置1s延迟它正常工作时,我需要无延迟地调用get_fb()。

这是我的get_fb:

    function get_fb(){
  var mla = 0;
  var mlo = 0;
  var mz = 0;
  var la = 0;
  var lo = 0;
  var na = 0;
  mla = document.getElementById('mla').value;
  mlo = document.getElementById('mlo').value;
  mz = document.getElementById('mz').value;
  la = document.getElementById('la').value;
  lo = document.getElementById('lo').value;
  na = document.getElementById('na').value;...

这是我的php输出:

        <input type="hidden" class="mla" id="mla" value="<?php echo $s1; ?>" />
        <input type="text" class="mlo" id="mlo" value="<?php echo $s2; ?>" />
        <input type="hidden" class="mz" id="mz" value="<?php echo $zoom; ?>" />
        <input type="hidden" class="os" id="os" value="<?php echo $ostan; ?>" />
        <input type="hidden" class="la" id="la" value="<?php echo $lat; ?>" />
        <input type="hidden" class="lo" id="lo" value="<?php echo $lon; ?>" />
        <input type="hidden" class="na" id="na" value="<?php echo $name; ?>" />

2 个答案:

答案 0 :(得分:1)

您应该这样称呼它:

from time import sleep
import threading

class example():

    def __init__(self):
        self.is_running = False

    def start(self):
        self.is_running = True
        self.loop_thread = threading.Thread(target=self.main_loop)

    def main_loop(self):
        while self.is_running:
            sleep(0.5)
            print(1)

    def stop(self):
        self.is_running = False
        print("SLEEEPINGGGGGGGGGGG")

a = example()
a.start()
sleep(3)
a.stop()

答案 1 :(得分:-1)

JavaScript实现async function声明。你要找的是这样的:

function showUser(user)
{
    // your logic
    get_fb_async();
}

async function get_fb_async()
{
    get_fb() = await showUser(user);
}

这必须根据get_fb()函数的行为方式而有所不同,但现在你有了逻辑。

更多详情:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function