开发者

Jquery incrementing number +1 0n page refresh

开发者 https://www.devze.com 2022-12-29 11:29 出处:网络
Hi guys i am trying to modify this code here. $(document).ready(function() { var randomImages = [\'img-1\',\'img-2\',\'img-3\',\'img-4\'];

Hi guys i am trying to modify this code here.

$(document).ready(function() { 

var randomImages = ['img-1','img-2','img-3','img-4'];

var rndNum = Math.floor(Math.random() * randomImages.length); 

$("div#bg-image").css({ background: "url(http://example.com/images/" + randomImages[rnd开发者_JAVA技巧Num] + ".jpg) no-repeat" });                                             
});

This works fine and gives me a random image each time which is great but what i need is for the image to increment +1 each time on page refresh.

This is because sometimes i can refresh 3 times and will still get the same image show when using Math.random(). I need to cancel the random and set +1 each time.

Any help


You need to store this number as a cookie. Then every time you refresh the page you know the previous number so you can either escape it or increment it by 1.

For reading/writing cookies use some of the plugins available, e. g. http://plugins.jquery.com/project/cookie

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号