我需要为function descramble()
制作动画。因此,它会更改第一个字符,然后在下一个字符上移动,依此类推。我尝试过使用setTimeout()
,但这只会延迟函数的启动。我只需要通过信函为ROT13转换字母设置动画。
我正在使用slice()
删除字符串的第一个元素,用ROT13替换它,然后用新字母和整个字符串替换字符串。我无法找到只删除段落第一个字母的方法。您可以单击jsfiddle中的第三个段落以查看转换。
$("#last-second-inside p:nth-child(3)").one('click', function() {
$(this).css('cursor', 'default');
var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
var strArr = str.split('');
var decodedArr = [];
var increaseNum = -1;
descramble();
function descramble() {
strArr.map(function(num) {
increaseNum++;
var currentLetter = num.charCodeAt();
if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
decodedArr.push(String.fromCharCode(currentLetter - 13));
} else {
decodedArr.push(String.fromCharCode(currentLetter + 13));
}
} else {
decodedArr.push(num);
}
var sliced = str.slice(increaseNum + 1, str.length - 1);
$("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
})
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
<p>You probably now understand that how binary works and how simple it is.</p>
<p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
<p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>
答案 0 :(得分:1)
不要使用map()
,而是descramble()
根据increaseNum
的值,一次只能处理一个字母。
运行descramble()
后,使用setTimeout()
在适当的时间间隔后再次调用它:
$("#last-second-inside p:nth-child(3)").one('click', function() {
$(this).css('cursor', 'default');
var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
var strArr = str.split('');
var decodedArr = [];
var increaseNum = 0; // changed from -1
descramble();
function descramble() {
var num = str[increaseNum++],
currentLetter = num.charCodeAt();
if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
decodedArr.push(String.fromCharCode(currentLetter - 13));
} else {
decodedArr.push(String.fromCharCode(currentLetter + 13));
}
} else {
decodedArr.push(num);
}
var sliced = str.slice(increaseNum + 1, str.length - 1);
$("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
if(increaseNum < str.length) {
setTimeout(descramble, 10);
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
<p>You probably now understand that how binary works and how simple it is.</p>
<p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
<p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>
答案 1 :(得分:1)
尝试使用.queue()
$("#last-second-inside p:nth-child(3)").one('click', function() {
$(this).css('cursor', 'default');
var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
var strArr = str.split('');
var decodedArr = [];
var increaseNum = -1;
descramble();
function descramble() {
$({}).queue("descramble", strArr.map(function(num) {
return function(next) {
increaseNum++;
var currentLetter = num.charCodeAt();
if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
decodedArr.push(String.fromCharCode(currentLetter - 13));
} else {
decodedArr.push(String.fromCharCode(currentLetter + 13));
}
} else {
decodedArr.push(num);
}
var sliced = str.slice(increaseNum + 1, str.length - 1);
$("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
// set duration of `setTimeout` here
setTimeout(next, 100)
};
})).dequeue("descramble")
}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
<p>You probably now understand that how binary works and how simple it is.</p>
<p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
<p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>
&#13;
答案 2 :(得分:0)
使用setInterval
,而不是setTimeout
。 setTimeout
只运行一次(在计时器到期后)。 setInterval
将以您设置的间隔继续射击,直到您逃离循环。
您可以将setTimeout
视为单个触发倒数计时器。 setInterval
可以被视为以您设置的间隔触发的循环。
答案 3 :(得分:0)
我相信你将不得不手动进行映射。你不能在像每个,map等迭代器中使用setTimeout或setInterval并得到你期望的结果,因为setTimeout是非阻塞的,所以它立即返回。你必须调用一个自动调用的函数,它一次转换1个字母,当你到达字符串的末尾时就会破坏逻辑,如下所示: How do I add a delay in a JavaScript loop?