如何将随机生成的数字保存到txt中



如何保存生成的最后5个数字的列表?我应该如何在以下代码中应用此函数?

<button onclick="document.body.innerHTML += (Math.floor(Math.random() * ( 989000000 - 988000000 ) + 987000000 ) + '<br>' );">Gerar Números</button><br>

如果你只想使用js,我发现这是用js创建一个文件然后我为您的目的编辑了脚本,查看结果:

let i=0;
let offset=-5;
let values="";
let $list = $("#list");
function generateRandom() {
var ran = Math.floor(Math.random() * ( 989000000 - 988000000 ) + 987000000 );
//textArea.value += ran + "n";
values += ran +"n";
$list.prepend('<li id="' + i++ + '">' + ran + '</li>');
if(offset>=0){
$("#" + offset).remove();
}
offset++;
}
(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(values);
link.style.display = 'block';
}, false);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="generateRandom()">Gerar Números</button>
<ul id="list"></ul>
<button id="create">Create file</button>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>

最新更新