gpt4 book ai didi

jQuery删除随机数组函数成功

转载 作者:行者123 更新时间:2023-12-01 04:14:32 25 4
gpt4 key购买 nike

我有这段脚本,但我无法弄清楚如何成功删除随机函数,以便数组一次流过每个数组项,而不是随机显示每个项。

$(window).ready(function() {
var helloArray = ["hello", "bonjour", "hola", "konnichiwa", "hujambo", "czesc", "hei", "ciao"];
$('#page_title').loadText( helloArray, 5000 ); // ( array, interval )
});

$.fn.loadText = function( textArray, interval ) {
return this.each( function() {
var obj = $(this);
obj.fadeOut( 'slow', function() {
obj.empty().html( random_array( textArray ) );
obj.fadeIn( 'fast' );
});
timeOut = setTimeout( function(){
obj.loadText( textArray, interval )
}, interval );
$("#text-reload").click( function(){
if( !obj.is(':animated') ) {
clearTimeout( timeOut );
// animation check prevents "too much recursion" error in jQuery
obj.loadText( textArray, interval );
}
});
});
}

//public function
function random_array( aArray ) {
var rand = Math.floor( Math.random() * aArray.length + aArray.length );
console.log(randArray);
var randArray = aArray[ rand - aArray.length ];
return randArray;
}

最佳答案

尝试

$(document).ready(function() {
var helloArray = ["hello", "bonjour", "hola", "konnichiwa", "hujambo",
"cześć", "hei", "ciao"];
$('#page_title').loadText(helloArray, 1000); // ( array, interval )
document.title = $('#page_title').text();
});
// custom jquery plugin loadText()
$.fn.loadText = function(textArray, interval) {
return this.each(function() {
var obj = $(this), intervalId, counter = 0;

function change() {
obj.fadeOut('slow', function() {
var text = textArray[counter++];
obj.html(text).fadeIn('fast');
document.title = text;

counter = counter >= textArray.length ? 0 : counter;
});
}

function start() {
intervalId = setInterval(change, interval);
};

start();

$("#text-reload").click(function() {
if (!obj.is(':animated')) {
clearInterval(intervalId);
start();
}
});
});
}

演示:Fiddle

关于jQuery删除随机数组函数成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com