尝试制作带有动画的随机字符串生成器
Trying to make a random string generator with animation
我正在尝试创建一个带有动画的字符串随机化器,但它不起作用。
我使用此函数按照浏览器选择的帧速率调用该函数:
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback, element){
window.setTimeout(callback, 1000 / 60);
};
})();
然后是我生成和显示字符串的方法:
function create(chars,string_length){
output = [];
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
output.push(randomstring);
document.getElementById('cb').innerHTML = (output.join(''));
}
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var length = 20;
requestAnimFrame( create(chars,length) );
它为我的div 'id' 生成一个字符串,但只有一次 - 所以函数工作但不是一直被调用 =/
为什么会这样?
当你使用 requestAnimationFrame 时,它只会运行一次函数。您需要修改create()
以再次调用 requestAnimationFrame 本身。此外,您还犯了另一个错误:您实际上是在调用create(chars, length)
然后将结果传递给requestAnimationFrame,而不是将函数create()
传递给requestAnimationFrame。这是一个应该有效的版本:
function create(chars,string_length){
output = [];
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
output.push(randomstring);
document.getElementById('cb').innerHTML = (output.join(''));
requestAnimFrame(function(){ create(chars,string_length); });
}
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var length = 20;
requestAnimFrame(function(){ create(chars,length) });
相关文章:
- 单击按钮时显示随机字符串
- 数组中的随机文本字符串
- 在本地存储中保存并返回随机生成的字符串
- 在JavaScript中生成安全、随机、Ascii编码的字符串
- 随机替换 JavaScript 或 jQuery 中文本块中的子字符串
- 需要从随机大小的字符串中提取字符串
- 使用Javascript从字符串中返回随机选择
- Javascript-如何从长度有时不是静态的随机字符串中获得3位数字
- 如何从字符串数组中获得8个随机唯一元素
- 如何在 JavaScript 中创建不断生成的随机生成的字符串
- 什么是检查随机字符串I've生成了has't是我以前生成的
- 从不同的数组中选择随机字符串
- 改进字符串的随机字符输出
- 输出中的随机字符串错误
- 如何使用 JavaScript 将字符添加到字符串的随机空间中
- 使用 JS 生成具有特定登录的随机字符串
- 使用 Node.js,如何将随机字符串路径重定向到主页
- 如何比较两个随机播放的字符串
- JQuery 字符串“随机发生器”聚类字符
- 字符串"ďťż"随机出现在我的页面上,但只在firefox中.IE和chrome没有这个问题