I want to generate the random id for the uploaded images with their file names.
my code is
var getUniqueMacroName = function(filename){
var Name = $.trim(filename.replace(/.[^.]+$/,'').replace(/ /g, "").replace(/[^\w]|_/g, "").toUpperCase());
var Ext = $.trim(filename.replace(/^.*\./,'').toUpperCase());
var mac= _.map($("#data").DataTable().data(), function(a){return a[0]});
var fileRand = Name.substring(0, (10 - fileExt.length));
while(_.contains(mac, ("GM" + fileRand + fileExt))){
var rand = Math.min(Math.floor((Math.random() * fileRand.length) + 5), (fileRand.length - 1));
fileRand = (fileRand.substring(0, rand) + rand + fileRand.substring(rand, fileRand.length)).substring(0, (10 - Ext.length));
}
return "GM" + fileRand + Ext;
}
If i upload the images like
gallery image 320 1.jpg
gallery image 320 2.jpg
gallery image 320 3.jpg
gallery image 320 4.jpg
gallery image 320 5.jpg
gallery image 320 6.jpg
the sixth image is execute infinite times in the while condition.This will happen when the uploaded image name have almost same.So the browser will be not respond.
Is there any way to generate the unique id for any type of file names(including the same filename) in javascript or jquery.