Timer javascript
Timer in javascript example source code will opening random link in random time.
// setTimeout()
// clearTimeout()
var start = new Date();
function timedCount()
{
var now = new Date();
var resultMilliSeconds = now.getTime() - start.getTime();
// create some div with id txt
document.getElementById('txt').innerHTML = resultMilliSeconds;
}
var t = setTimeout("timedCount()",1000);
// clearTimeout(t); // for stopping create button with onclick clearTimeout(t)
Použití timeru v javasciptu. Příklad otvírá náhodně náhodný odkaz v okně prohlížeče za náhodný časový interval.
var c=0;
var t = new String().toString();
var t2 = new String().toString();
var timer_is_on=0;
var newWindow;
function open2(url, opt){
if (opt == 0) // current window
window.location = url;
else if (opt == 1){ // new window
if(newWindow) newWindow.open(url,"_parent");
else
newWindow = window.open(url);
}
else if (opt == 2) // background window
{window.open(url); self.focus();}
}
var ar = new Array (
"https://www.okhelp.cz/polevky/"
,"https://www.okhelp.cz/auto-moto/"
,"https://www.okhelp.cz/pc-mobily/"
);
function timedCount()
{
var randomnumber=Math.floor(Math.random()*ar.length)
open2(ar[randomnumber],1);
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000 * (randomnumber + 4));
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
clearTimeout(t2);
timer_is_on=0;
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
77LW NO topic_id
AD
Další témata ....(Topics)
global je klíčové slovo, které zajistí viditelnost proměnné například uvnitř funkcí.
$a = 5;
$b = 2;
function Secti()
{
global $a, $b;
$b = $a + $b;
}
Secti();
echo $b;// 7
// pred vsemi include file
$varGlob = 10;
include('some.inc');
$GLOBALS; // pole ktere obsahuje SERVER i vsechny uzivatelovi globalni promenne
function PrintFC()
{
$a = $GLOBALS['varGlob']; // ziskame globalni promennou
$GLOBALS['varGlob'] = 20; // muzeme prepsat jeji hodnotu
print $a; // 10
}
PrintFC();
print $varGlob; // 20
var 1_radian_to_degree = 180 / Math.PI; // 57,295779513082320876798154814105 degree
var 1_degree_to_radian = Math.PI / 180; // 0,01745329251994329576923690768489 radian
var 360_degree = 2 * Math.PI * 1_radian_to_degree;
var degree = 36.86;
var opposite = 600;
var hypotenuse = 1000;
var adjacent = 800;
// degree
// sin 45° == 0.7071067811865475;
var sin = Math.sin(45 * Math.PI / 180.0)
//sin == 0.7071067811865475;
// vypocet prepony (hypotenuse), kdyz zname
// protilehlou (opposite) a uhel (degree)
hypotenuse2 = opposite/ Math.sin(degree * Math.PI / 180.0);
// vypocet protilehle (opposite)
opposite2 = hypotenuse * Math.sin(degree * Math.PI / 180);
// vypocitat uhel alfa z velikosti protilehle a prepony ve stupnich (degree)
degree2 = Math.asin(opposite / hypotenuse)* (180/Math.PI); // 1rad
// velikost prepony pomoci prilehle (adjacent) strany a cos a uhlu v stupnich (degree)
hypotenuse3 = adjacent / Math.cos(degree * Math.PI / 180);
// velikost prilehle (adjacent) strany pomoci prepony a cos a uhlu v stupnich (degree)
adjacent2 = hypotenuse * Math.cos(degree * Math.PI / 180);
tan = Math.tan(degree * Math.PI/180); // 45° == 0.9999999999999
degree3 = Math.atan(opposite / adjacent)* (180/Math.PI); // 1rad
adjacent3 = opposite / Math.tan(degree * Math.PI / 180);
opposite3 = adjacent * Math.tan(degree * Math.PI / 180);
$country[0] = "cz"
$country[1] = "sk"
$country[2] = "de"
$country[3] = "uk"
$country[4] = "fr"
// this do same
$country[] = "cz"
$country[] = "sk"
$country[] = "de"
$country[] = "uk"
$country[] = "fr"
// or
$country = array('cz','sk','de','uk','fr');
// get array items count
$length = count($country);
for($i=0;$i<$length;$i++){
print $country[$i].'<br>';// cz sk de uk fr
}
// or like this
$country[50] = 'ca';
$country[10] = 'fr';
$country[20] = 'us'
// now country will index 51
$country[] = 'ru';
// or
$country = array('5'=>'cz','sk','de','uk','fr');
/*
Array
(
[5] => cz
[6] => sk
[7] => de
[8] => uk
[9] => fr
)
*/
// you can do this
$country["cz"] = 1571;
$country["sk"] = 1572;
$country["de"] = 1573;
$country["uk"] = 1574;
$country["fr"] = 1575;
print $country["uk"]; // 1574
// print all items key and value
foreach ($country as $key => $value){
print $key; // cz, sk, de, uk, fr
print $value; // 1571, 1572, 1573, 1574, 1575
}
Tthis code works in all previous versions of browsers
Pause code by alert
Set timeout
Pause code by alert
var n = 1;
alert(n);
n = 5;
alert(n);
Set timeout
var n = 5;
// wait 5 second / do next code
// next code put into function and call this function by setTimeout
setTimeout("nextCodeFc (n)",5000); // wait for 5000 millisecond == 5 second and execute
// how put string as parameter into setTimeout
var str = "Hello string parameter!";
setTimeout('nextCodeFc("str")',3000); // produce "str"
setTimeout('nextCodeFc("'+str+'")',3000); // produce "Hello string parameter!"
setTimeout("nextCodeFc(str)",3000); // produce Hello string parameter!
function nextCodeFc (n){
// do work .........
alert(n);
}
CORS-Cross-Origin-Resource-Sharing Header set Access-Control-Allow-Origin Source Code Example
Usefull Links here
https://www.w3.org/wiki/CORS_EnabledApache .htaccess Allow File Types
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch ".(bmp|cur|gif|ico|jpe?g|png|svgz?|webp)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>
.htaccess which is closest to the file!!!!!
Date: 17.06.2020 - 08:09Editace: 11.6.2020 - 22:30
Počet článků v kategorii: 77
Url:timer-javascript