Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击一次执行js函数_Javascript_Json_Function - Fatal编程技术网

Javascript 单击一次执行js函数

Javascript 单击一次执行js函数,javascript,json,function,Javascript,Json,Function,提交搜索框时,我执行2个API函数 document.getElementById('getWeather').onclick = function(){ // init data stream getAPIdata(); getAPIWeerdata(); }; 第一个getAPIdata()用于搜索框本身,当然,当用户再次提交时,该值可能会更改。所以这可以执行多次 另一方面,第二个是getAPIWeerdata()在用户提交搜索框时执行一次 我可以创建类似于getAPIWeer

提交搜索框时,我执行2个API函数

document.getElementById('getWeather').onclick = function(){
  // init data stream
  getAPIdata();
  getAPIWeerdata();
};
第一个
getAPIdata()
用于搜索框本身,当然,当用户再次提交时,该值可能会更改。所以这可以执行多次

另一方面,第二个是
getAPIWeerdata()在用户提交搜索框时执行一次


我可以创建类似于
getAPIWeerdata()的内容吗;。ExecuteOnce

一种解决方案是在调用onclick函数时使用设置为false的布尔值,并且仅当bool为true时才调用
getAPIWeerdata()
。确保在将bool设置为false之前调用
getAPIWeerdata()

因此:


一种解决方案是在调用onclick函数时使用一个设置为false的布尔值,并且仅当bool为true时才调用
getAPIWeerdata()
。确保在将bool设置为false之前调用
getAPIWeerdata()

因此:


在相应的dom元素上设置
data-
属性:

document.getElementById('getWeather').onclick = function(){
    // init data stream
    getAPIdata();
    if (!document.getElementById('getWeather').hasAttribute('data-inited')) {
        getAPIWeerdata();
        document.getElementById('getWeather').setAttribute('data-inited', 1'); // Value does not matter
    }
}

在相应的dom元素上设置
data-
属性:

document.getElementById('getWeather').onclick = function(){
    // init data stream
    getAPIdata();
    if (!document.getElementById('getWeather').hasAttribute('data-inited')) {
        getAPIWeerdata();
        document.getElementById('getWeather').setAttribute('data-inited', 1'); // Value does not matter
    }
}

检查lodash library once函数或检查此处的答案:检查lodash library once函数或检查此处的答案:是的,我一发布此问题就意识到了这一点,无论如何,谢谢!是的,我一发布这个问题就意识到了这一点,无论如何,谢谢!