简体   繁体   English

JavaScript如何在函数中创建事件链,因此一个函数要等到另一个函数完成后才能启动?

[英]How can javascript create a chain of events in a function, so one function does not start until another is completed?

Working on code for personal site and I'm trying to rewrite code that is on the Internet so it's even easier to use for me. 正在处理个人站点的代码,我正在尝试重写Internet上的代码,因此对我来说更容易使用。

The code is from http://cssdeck.com/labs/7bx7mmcm 该代码来自http://cssdeck.com/labs/7bx7mmcm

How would it be possible to add a marker so that after fileToLoad.click(); 如何添加标记,以便在fileToLoad.click()之后; is pushed through and completed, then loadFileAsText() goes? 被推入并完成,然后loadFileAsText()去吗? Much thanks in advance! 提前非常感谢!

<textarea id="BoxText4Load"></textarea>
<input type="file" id="fileToLoad" class="hidden">
<button id="loader">Loader</button>
<input id="inputFileNameToSaveAs" style="display: none;"></input>
<button onclick="saveTextAsFile()">Save file</button>

var loaderbutton = document.getElementById("loader");
loaderbutton.addEventListener('click', function(event){
fileToLoad.click();
setTimeout(function() {loadFileAsText(); }, 3000);
} , false)

Code below directly is from above link: 下面的代码直接来自上面的链接:

"inputFileNameToSaveAs"

function saveTextAsFile(){
var textToWrite = document.getElementById("BoxText4Load").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.URL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}

downloadLink.click();
}

function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}

function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];

var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("BoxText4Load").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}

I like to use events whenever possible. 我喜欢尽可能使用事件 JavaScript allows us to attach functions that are triggered when certain events occur such as clicking an element or when an element changes. JavaScript允许我们附加在某些事件(例如单击元素或元素更改)发生时触发的功能。

You could add a click event for #fileToLoad . 您可以为#fileToLoad添加click事件。 This will fire as soon as the element is clicked. 单击该元素后将立即触发。

document.getElementById("loader").addEventListener('click', function(event){
  loadFileAsText()
}

If you want the function to run after the file has been chosen, then you can use a change event. 如果希望函数在选择文件后运行,则可以使用更改事件。

 function loadFileAsText() { console.log('loadFileAsText running!') } var loaderbutton = document.getElementById('loader') var fileToLoad = document.getElementById('fileToLoad') loaderbutton.addEventListener('click', function(event){ console.log('loaderbutton clicked!') fileToLoad.click() } , false) fileToLoad.addEventListener('change', function(event){ loadFileAsText() }) 
 <textarea id="BoxText4Load"></textarea> <input type="file" id="fileToLoad" class="hidden"> <button id="loader">Loader</button> <input id="inputFileNameToSaveAs" style="display: none;"></input> <button onclick="saveTextAsFile()">Save file</button> 

You could use a callback function (like HarlemSquirrel's comment). 您可以使用回调函数(例如HarlemSquirrel的注释)。

Assuming there is a fair amount of data posting and retrieval, you could also make get and post requests and define how the client handles the response. 假设有大量的数据发布和检索,您还可以发出获取和发布请求,并定义客户端如何处理响应。 I included a vanillaJS link as it seems you're writing that way. 我包括了vanillaJS链接,因为您似乎是用这种方式编写的。 A promise, as @Winter mentioned might be more convenient as it reduces the lines you'd write. @Winter提到的一个promise可能会更方便,因为它减少了您要编写的行。 I had heard a recent/abbreviated application of promises is fetch but I haven't used that yet. 我听说最近/缩写的Promise应用已获取,但我还没有使用过。 David Walsh has a concise comparison of the vanillaJS and fetch way. David Walsh对vanillaJS和访存方式进行了简要的比较

Thanks HarlemSquirrel for the tip on "change". 感谢HarlemSquirrel提供有关“更改”的提示。 It led to Change- Event Reference and to find a way to target my input: How can I use querySelector on to pick an input element by name? 它导致了Change-Event Reference,并找到了一种针对我的输入的方法: 如何使用querySelector来按名称选择输入元素?

The way I wanted the interface to work is to choose the file and then have it automatically load: 我希望界面起作用的方式是选择文件,然后使其自动加载:

document.addEventListener('DOMContentLoaded',function() {
    document.querySelector('input[name="filetoloadname"]').onchange=changeEventHandler;
},false);

function changeEventHandler(event) {
loadFileAsText();
}


<body>
<input type="file" id="fileToLoad" name="filetoloadname">
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM