You are viewing our Forum Archives. To view or take place in current topics click here.
Firefox Issue with JavaScript Function
Posted:

Firefox Issue with JavaScript FunctionPosted:

Left
  • Summer 2018
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
I have a strange issue with Firefox

I have a site [ Register or Signin to view external links. ] the site embeds a PHP script that gets a random webm from a directory and then shoots it out. (you can see the script working here [ Register or Signin to view external links. ] ) and when the video is finished on the page it will do this javascript function

   function reloadVid(e) {
       
      console.log("Fin")
      history.go(0);
    }


(reloading the web page)

So the issue is this works awesome with chrome and it keeps giving you random videos every time the old one ends but firefox keeps repeating the same video. I don't know if this is something to do with firefoxs caching or something but if you could shed any light on the issue that would be awesome.
#2. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
I'm not hugely sure but because of the way computers generate random numbers, it may be possible that it's just generating the same number each time?

Try generating a random number based on the current time(something that will always change) to see if you get a different value with Firefox.
#3. Posted:
Left
  • Summer 2021
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
-Deano wrote I'm not hugely sure but because of the way computers generate random numbers, it may be possible that it's just generating the same number each time?

Try generating a random number based on the current time(something that will always change) to see if you get a different value with Firefox.


The script doesn't generate a random number it generates a random filename thats in the directory.
#4. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Left wrote The script doesn't generate a random number it generates a random filename thats in the directory.


What are you using to generate a random one?
#5. Posted:
Left
  • Summer 2021
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
Status: Offline
Joined: Jun 30, 201113Year Member
Posts: 1,010
Reputation Power: 49
-Deano wrote
Left wrote The script doesn't generate a random number it generates a random filename thats in the directory.


What are you using to generate a random one?


<?php

// start session handling
session_start();

$folder = 'vid/';

// Space seperated list of extensions
$exts = 'webm webmhd';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2

$rand = mt_rand(0, $i); // $i was incremented as we went along

// check if vid in session array, if found, get new rand value and try again
while (in_array($files[$rand], $_SESSION['shown_vids'])) {
  $rand = mt_rand(0, $i);
}

header('Location: '.$folder.$files[$rand]); // Voila!

?>
#6. Posted:
var
  • TTG Senior
Status: Offline
Joined: Dec 24, 201211Year Member
Posts: 1,498
Reputation Power: 79
Status: Offline
Joined: Dec 24, 201211Year Member
Posts: 1,498
Reputation Power: 79
How well does it work in Safari, and maybe IE?
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.