<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript随机邀请码生成</title>
</head>
<body>
<textarea name="textarea" cols="100" rows="50"></textarea>
<script>// 在光标处插入字符串
// myField 文本框对象
// myValue 要插入的值
function insertAtCursor(myField, myValue)
{
//IE support
if (document.selection)
{
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
sel.select();
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0')
{
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
// save scrollTop before insert
var restoreTop = myField.scrollTop;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
if (restoreTop > 0)
{
// restore previous scrollTop
myField.scrollTop = restoreTop;
}
myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
} else {
myField.value += myValue;
myField.focus();
}
}
var number = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
step = 25,
str = "YQM-",
arr = "",
z="",
k=0,
set = null;
var text = document.getElementsByTagName('textarea')[0],
v = text.value;
function rd(n,m){
var c = m-n+1;
return Math.floor(Math.random() * c + n);
}
set = setInterval(()=>{
k++;
arr="";
for(var i=0;i<step;i++){
arr+=number[rd(0,25)];
}
z=str+arr;
insertAtCursor(text,z+"\n");
k==10?clearInterval(set):'';
},1);
</script>
</body>
</html>